1

I have a java object 'star' that consists of two columns, string name (the name of the star) and string List fans (the list of fans of this star). I'd like to persist this class using JPA1 or hibernate. I've done so using the annotation @collectionOfElements on the list. It works fine, and creates two tables.

Now I'd like to get all stars whose fans are 'alice' or 'bob' or 'charlie'. How can I do that in the easiest way (only one query rather than 3, and without using 'OR' statements if possible), using jpa queries (hibernate if it's a must), and without retrieving the whole list of fans ?

Thanks

3
  • Why would you want to avoid using or? This is meant to be used in such cases. Commented Jan 29, 2013 at 20:08
  • If all my other requirements are met, I think I can live with a OR. Commented Jan 29, 2013 at 20:14
  • from star where fans in ('alice', 'bob', 'charlie') Commented Jan 29, 2013 at 20:15

1 Answer 1

1

The following query should help you:

select s.* from star s where s.fans.name in ('alice', 'bob', 'charlie')
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't seem to work. The fans column doesn't exist, it's persisted in another table.
It's kinda hard to guess without having your code. Could you provide entities and mappings? My example is written in JPAQL, so I assumed your Star object has a List<Fan> fans attribute.

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.