Hibernate 标准:左外连接,对两个表都有限制

发布于 2024-08-20 18:44:26 字数 1288 浏览 7 评论 0原文

我正在执行 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") // Product:数据库表的名称

这是我收到的异常(如果我理解正确的话)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

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

问题可能与以下

更新:

相同:这: https://forum.hibernate.org/viewtopic.php?p=2393694

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我还不会笑 2024-08-27 18:44:26

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

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

请注意,您正在执行左外连接,并对该列进行限制...本质上使其成为内连接。如果您还想要没有产品的申请人,那么您还必须检查空产品。

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.

最单纯的乌龟 2024-08-27 18:44:26

更新:

.CreateAlias("products", "p", JoinType.LEFT_OUTER_JOIN)
.Add(Restrictions.Eq("p.inventedName", inventedName));

Update:

.CreateAlias("products", "p", JoinType.LEFT_OUTER_JOIN)
.Add(Restrictions.Eq("p.inventedName", inventedName));
原来是傀儡 2024-08-27 18:44:26

我解决了这个问题,创建了一个新的标准,在这两种情况下我都可以使用 LEFT_OUTER_JOIN 。

I solved this my problem creating a new criteria and I could use LEFT_OUTER_JOIN in both cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文