Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Criteria: Left Outer Join with restrictions on both tables

I am doing a LEFT OUTER JOIN, but I am only able to apply Restrictions on the first table. Is there a way ti apply on the second table as well?

Here is my code:

Criteria criteria = this.crudService
        .initializeCriteria(Applicant.class).setFetchMode("products",
              FetchMode.JOIN);.

This works (applicant has an applicantName property):

criteria.add(Restrictions.eq("applicantName", "Markos")

Neither of these works (product has a productName property)

criteria.add(Restrictions.eq("productName", "product1")

criteria.add(Restrictions.eq("products.productName", "product1") // products: the name of the property criteria.add(Restrictions.eq("Product.productName", "product1") // Product: the name of the DB table

And this is the exception I am receiving saying (if I understand correctly) that the productName property does not exist in Applicant:

EJB Exception: ; nested exception is: org.hibernate.QueryException: could not resolve property: products.inventedName of: org.myCompany.applicant.entity.Applicant; nested exception is: org.hibernate.QueryException: could not resolve property: products.inventedName of: org.myCompany.applicant.entity.Applicant

I tried to use an alias, but this generated an INNER JOIN, instead of the LEFT OUTER JOIN I want.

How can I apply restrictions on both tables?

UPDATE:

Issue is probably the same as this: https://forum.hibernate.org/viewtopic.php?p=2393694

like image 727
Markos Fragkakis Avatar asked Feb 05 '10 15:02

Markos Fragkakis


People also ask

How can we join multiple tables in hibernate criteria?

Criteria in Hibernate can be used for join queries by joining multiple tables, useful methods for Hibernate criteria join are createAlias(), setFetchMode() and setProjection() Criteria in Hibernate API can be used for fetching results with conditions, useful methods are add() where we can add Restrictions.

What is restriction in hibernate criteria?

The Criteria interface makes it easy to selectively fetch the data on the basis of conditions in the select query. The Restriction class in hibernate provide several methods that can be used as conditions (also known as Criterion). These conditions are added to a criteria object with the add() method.


1 Answers

Update:

.CreateAlias("products", "p", JoinType.LEFT_OUTER_JOIN)
.Add(Restrictions.Eq("p.inventedName", inventedName));
like image 191
alden8 Avatar answered Oct 22 '22 20:10

alden8