Hibernate 标准:左外连接,对两个表都有限制
我正在执行 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。
问题可能与以下
更新:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 createalias 中指定左外连接...
请注意,您正在执行左外连接,并对该列进行限制...本质上使其成为内连接。如果您还想要没有产品的申请人,那么您还必须检查空产品。
You can specify left outer join in the createalias...
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.
更新:
Update:
我解决了这个问题,创建了一个新的标准,在这两种情况下我都可以使用 LEFT_OUTER_JOIN 。
I solved this my problem creating a new criteria and I could use LEFT_OUTER_JOIN in both cases.