本文介绍了休眠条件:左外部连接对两个表都有限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个LEFT OUTER JOIN,但我只能对第一个表应用限制。还有一种方法适用于第二张桌子吗?



这是我的代码:

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

这有效(申请人有一个applicantName属性):

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

这些作品都没有(产品具有productName属性)

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

criteria.add(Restrictions.eq(products.productName product1)//产品:属性的名称
criteria.add(Restrictions.eq(Product.productName,product1)//产品:数据库表的名称



这是我收到的异常(如果我理解正确),申请人中不存在productName属性:

  EJB异常:嵌套异常是:org.hibernate.QueryException:无法解析属性:products.inventedName:org.myCompany.applicant.entity.Applicant;嵌套异常是:org .hibernate.QueryException:无法解析lve property:products.inventedName:org.myCompany.applicant.entity.Applicant 

我尝试过使用一个别名,但是这产生了一个INNER JOIN,而不是我想要的LEFT OUTER JOIN。



如何对两个表应用限制?



更新:

问题可能与此相同:

$您可以在createalias中指定左外连接...

 

.CreateAlias(products,p,NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.Add(Restrictions.Eq(p.inventedName,inventedName));

请注意,您正在对该列进行限制,它是一个内部连接。如果您还希望没有产品的申请人,那么您也必须检查空产品。


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 propertycriteria.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

解决方案

You can specify left outer join in the createalias...

.CreateAlias("products", "p", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.Add(Restrictions.Eq("p.inventedName", inventedName));

Be aware that you are doing a left outer join with a restriction on that column...essentially making it an inner join. If you also want applicants without products then you'll have to check for null product too.

这篇关于休眠条件:左外部连接对两个表都有限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 04:39