Search code examples
springhibernatenamed-query

Hibernate Spring Named query to query in a list of objects


I have an Entity called ArtWork and the Entity has attribute List<Style> styles, the list may have n number of styles.

Style has attribute called title

I require a Hibernate query that returns all artworks which has a style title="Abstract"

-Thanks for your help


Solution

  • Single title:

    select a from ArtWork a inner join a.styles style where style.title = 'Abstract'
    

    Multiple titles:

    Provide a named parameter list.

    List<String> titles = ... // Your titles
    session.createQuery("from ArtWork a inner join a.styles style where style.title in (:titles)").setParameterList("titles", titles);