Project an Entity from a relationship with Hibernate Criteria

泪湿孤枕 提交于 2019-11-30 21:20:37

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/14970524/project-an-entity-from-a-relationship-with-hibernate-criteria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!