通过与Hibernate Criteria的关系项目实体 [英] Project an Entity from a relationship with Hibernate Criteria

查看:87
本文介绍了通过与Hibernate Criteria的关系项目实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为条件表达式的结果,我无法获取相关实体对象的列表。我有两个具有多对多关系的对象,使得ObjectA - ObjectB可以将ObjectA的单个实例绑定到ObjectB的多个实例,ObjectB的实例可以绑定到ObjectA的多个实例。这种关系存储在典型的连接表中,但出于遗留原因,对象模型是这样的,即ObjectB不直接了解它与ObjectA的关系。我试图创建一个条件表达式来获取与任何ObjectA通过连接表对象关联的所有实例ObjectB,如下所示:

  getDetachedCriteria(ObjectAObjectB.class)
.setFetchMode(objectB,FetchMode.JOIN)
.setProjection(Projections.property(objectB));

然而,这并不像预期的那样工作,因为它似乎Projection API只支持投影标量属性和不是实体对象。是否有可能通过Projections或其他Criteria API指定这种类型的选择? 解决方案

你不能用Hibernate Criteria API。这可能在HQL或JPA2 Criteria API中,但不能使用Hibernate Criteria API。


I'm having trouble getting a list of related entity objects as the result of a criteria expression. I have two objects with a many to many relationship such that ObjectA <-> ObjectB where a single instance of ObjectA can be tied to a multiple instances of ObjectB, and an instance of ObjectB maybe tied to multiple instances of ObjectA. This relationship is stored in your typical join table, but for legacy reasons the object model is such that ObjectB has no direct knowledge of it's relationship to ObjectA. I'm trying to create a criteria expression to get all of the instances ObjectB that are related to any ObjectA through the join table object with the following:

getDetachedCriteria(ObjectAObjectB.class)
.setFetchMode("objectB", FetchMode.JOIN)
.setProjection(Projections.property("objectB"));

However this doesn't work as expected, as it appears the Projection API only supports projecting scalar properties and not Entity Objects. Is it possible to specify this type of selection through Projections or some other Criteria API?

解决方案

You can't do that with the Hibernate Criteria API. It's possible in HQL or in the JPA2 Criteria API, but not using the Hibernate Criteria API.

这篇关于通过与Hibernate Criteria的关系项目实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆