3

Consider an entity

@Entity
class Book{
  ...
  @ElementCollection(fetch = FetchType.EAGER)
  List<String> tags = new ArrayList<String>();
  ...
}

What should be the hibernate query so one can get all the books containing any multiple tag "hobbit,sherlock,fiction"(OR operation splitted by comma).Any query(HQL,Criteria or raw SQL) will work,though Criteria is prefered.

1 Answer 1

2

I solved the problem by myself but not using pure criteria

criteria.add(Restrictions.sqlRestriction("BOOK_ID IN " +
            "(SELECT BOOK_ID FROM Book_tags " + 
            "WHERE tags ='" + tag + "')"));

Please note the single quote surrounding the query parameter 'tag' .Pure criteria query is always welcomed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.