Entity Graph attribute resolution not based on selected entity

Description

I was trying to fetch an entities association and have that fully initialized (will be serialized to JSON):

Applying a Named/Ad Hoc Fetch Graph yields an Exception. E.g. for this Graph:

I get:

org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,collection join,fetch join,fetch non-lazy properties,classAlias=u,role=com.example.assocFetchGraph.persistence.User.permissions,tableName={none},tableAlias=permission3_,origin=null,columns={,className=null}}]

That is because both classes have a `permissions` property. Otherwise I would get an error saying the attribute was not found.

Expected behavior would be, that the Fetch Graph's attributes would be resolved relative to Group, not User. (But the JPA spec doesn't seem to be very clear here).

I have created a reproducing example: https://github.com/cptwunderlich/assocEntityGraphMRE

We had a discussion about this on Zulip #hibernate-user "EntityGraph resolution"
I also created a StackOverflow question: https://stackoverflow.com/questions/63015923/jpa-select-association-and-use-namedentitygraph

I provde an excerpt of the code here for your convenience:

Stack Trace is:

 

The repository also contains a test case where I JOIN FETCH permissions and that works. Interestingly, in my actual code, I have and additional JOIN FETCH for a ManyToOne and I get the above Exception for that additional property.

Environment

Spring Boot 2.3.1.RELEASE, Java 11 (Adopt OpenJDK), H2 DB & Postgresql 10, Windows 10

Activity

Show:
Nathan Xu
July 22, 2020, 11:37 PM

Thanks for the complete bug reporting. It is really helpful to fix the bug quickly. PR created at:

Nathan Xu
July 26, 2020, 11:12 PM
Edited

I think we’d better separate the concern. How to support `select u.groups from User u` in terms of entity graph is a big question as you can see from the zulip chat. As you have known, we only support very limited HQL to enforce entity graph (e.g. select u from User u where ....).

Entity Graph is awkward feature in Hibernate v5 for the late feature is not elegantly implemented in existing infrastructure. However, we will reconsider the issue in v6 whose design makes nontrivial refactoring possible. The combination of entity graph and HQL is a great gray area and we need thorough design if we wanna support applying Group graph rather than User graph for select u.groups from User u.

Assignee

Nathan Xu

Reporter

Benjamin Maurer

Fix versions

Labels

None

backPortable

Backport?

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure