[hibernate] Hibernate Criteria Join with 3 Tables

I am looking for a hibernate criteria to get following:

Dokument.class is mapped to Role roleId

Role.class has a ContactPerson contactId

Contact.class FirstName LastName

I want to search for First or LastName on the Contact class and retrieve a list of Dokuments connected.

I have 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 could not resolve property "LastName" for class "Dokument"

Can someone explain why the join searches on Dokument and not on all joined tables? Thanks in advance for all the help!

This question is related to hibernate join criteria

The answer is


The fetch mode only says that the association must be fetched. If you want to add restrictions on an associated entity, you must create an alias, or a subcriteria. I generally prefer using 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 is of course well explained in the Hibernate reference manual, and the javadoc for Criteria even has examples. Read the documentation: it has plenty of useful information.


Examples related to hibernate

Hibernate Error executing DDL via JDBC Statement How does spring.jpa.hibernate.ddl-auto property exactly work in Spring? Error creating bean with name 'entityManagerFactory' defined in class path resource : Invocation of init method failed JPA Hibernate Persistence exception [PersistenceUnit: default] Unable to build Hibernate SessionFactory Disable all Database related auto configuration in Spring Boot Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] HikariCP - connection is not available Hibernate-sequence doesn't exist How to find distinct rows with field in list using JPA and Spring? Spring Data JPA and Exists query

Examples related to join

Pandas Merging 101 pandas: merge (join) two data frames on multiple columns How to use the COLLATE in a JOIN in SQL Server? How to join multiple collections with $lookup in mongodb How to join on multiple columns in Pyspark? Pandas join issue: columns overlap but no suffix specified MySQL select rows where left join is null How to return rows from left table not found in right table? Why do multiple-table joins produce duplicate rows? pandas three-way joining multiple dataframes on columns

Examples related to criteria

Hibernate Criteria Join with 3 Tables Hibernate Criteria Restrictions AND / OR combination How to properly express JPQL "join fetch" with "where" clause as JPA 2 CriteriaQuery? Hibernate Criteria for Dates Getting a count of rows in a datatable that meet certain criteria How to get SQL from Hibernate Criteria API (*not* for logging) How to get distinct results in hibernate with joins and row-based limiting (paging)? JPA and Hibernate - Criteria vs. JPQL or HQL Hibernate Query By Example and Projections