简体   繁体   中英

Project an Entity from a relationship with Hibernate Criteria

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM