Hibernate - restriction for a class in the list

I am trying to undo a list of elements that have a specific type of element in a set.

For instance:

<class name="Owner" table="OWNER"> <id name="id" column="OWNER_ID" /> <set name="cats" table="OWNER_CATS" lazy="false"> <key column="OWNER_ID" /> <many-to-many class="Cat" /> </set> 

 <class name="Cat" table="CAT" discriminator-value="C"> <id name="id" column="CAT_ID" /> <discriminator column="type" type="character" /> <subclass name="Lion" discriminator-value="L"> <property name="teeth" /> </subclass> </class> 

Using restrictions, how can I get a list of owners who have lions as pets?

I tried something in the following lines to no avail:

 criteria.createCriteria("cats").add(Restrictions.eq("class", Lion.class)); 
+4
source share
1 answer

At the moment (although I'm not sure if this is intentional), how it works, you need to specify the value of the discriminator as the value of the constraint, and not the class itself, therefore:

 criteria.createCriteria("cats").add(Restrictions.eq("class", "L")); 

in your example.

+5
source

Source: https://habr.com/ru/post/1312183/


All Articles