Sleeping Criteria Join 3 Tables

I am looking for criteria for sleep mode:

Dokument.class maps to role Role identifier

Role.class has a ContactID contact person

Contact.class Name LastName

I want to find First or LastName in the Contact class and get a list of connected Dokuments.

I tried something like this:

session.createCriteria(Dokument.class) .setFetchMode("role",FetchMode.JOIN) .setFetchMode("contact",FetchMode.JOIN) .add(Restrictions.eq("LastName","Test")).list(); 

I get an error, I can not resolve the property "LastName" for the class "Document"

Can someone explain why search by query in Document and not on all joined tables? Thank you in advance for your help!

+50
join hibernate criteria
Jan 04 '12 at 11:50
source share
1 answer

In sampling mode, it is indicated that the association should be obtained. If you want to add restrictions to the associated object, you must create an alias or sub-criteria. I usually prefer to use aliases, but YMMV:

 Criteria c = session.createCriteria(Dokument.class, "dokument"); c.createAlias("dokument.role", "role"); // inner join by default c.createAlias("role.contact", "contact"); c.add(Restrictions.eq("contact.lastName", "Test")); return c.list(); 

This, of course, is well explained in the Hibernate reference manual, and javadoc even has examples for the criteria . Read the documentation: it has a lot of useful information.

+102
Jan 04 2018-12-12T00:
source share



All Articles