Hibernate Criteria:Left Outer Join对两个表都有限制

Mar*_*kis 9 hibernate join criteria hibernate-criteria

我正在进行LEFT OUTER JOIN,但我只能在第一个表上应用Restrictions.有没有办法在第二张桌子上申请?

这是我的代码:

Criteria criteria = this.crudService
        .initializeCriteria(Applicant.class).setFetchMode("products",
              FetchMode.JOIN);.
Run Code Online (Sandbox Code Playgroud)

这工作(申请人有applicantName属性):

criteria.add(Restrictions.eq("applicantName", "Markos")
Run Code Online (Sandbox Code Playgroud)

这些都不起作用(产品具有productName属性)

criteria.add(Restrictions.eq("productName", "product1")
Run Code Online (Sandbox Code Playgroud)

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

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

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
Run Code Online (Sandbox Code Playgroud)

我尝试使用别名,但这会生成一个INNER JOIN,而不是我想要的LEFT OUTER JOIN.

如何对两个表格施加限制?

更新:

问题可能与此相同:https: //forum.hibernate.org/viewtopic.php?p = 2393694

dot*_*joe 18

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

.CreateAlias("products", "p", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.Add(Restrictions.Eq("p.inventedName", inventedName));
Run Code Online (Sandbox Code Playgroud)

请注意,您正在执行左外连接,并对该列进行限制...实际上使其成为内连接.如果您还希望申请人没有产品,那么您也必须检查空产品.