Have multiple bag fetches revert to subselect fetching for all but one of the bags

Description

Follow on to HHH-1413. Multiple bag fetches were simply disallowed as the resolution to that particular case in the interest of working around that issue.

The correct longer term solution is to not fetch all the bags at once, ideally reverting to subselect fetching for all but one of the bags.

Environment

None

Activity

Show:
Stephen Neal
October 8, 2008, 5:04 PM

There's a simple, although non-JPA-standard, fix for this.

Specify the following Hibernate annotation alongside your OneToMany field. like so:

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<Child> children = new ArrayList<Child>();

Then hibernate will use a subselect rather than a join which is the cause of the issue!

Steve Ebersole
October 3, 2013, 11:47 AM

The bulk of the prerequisite work needed for this issue is covered by the LoadPlan work under

Steve Ebersole
October 3, 2013, 11:51 AM

True that is one work around. The other is to specify the list index column which is not a Hibernate-specific annotation anymore (JPA offers a standardized one):

The trouble here is the distinction between a List (true List behavior, persistent ordering) versus what Hibernate terms a bag. Specifying the OrderColumn (the "list index" column) makes this a true List

Ka Wu
March 19, 2014, 2:38 AM
Edited

How does @OrderColumn work when the index column is part of the primary key, which is not allowed - at least least according to the JPA?: http://stackoverflow.com/q/10054400/396732 Is it nontheless supported by Hibernate? Otherwise the latter workaround isn't one, which makes reverting to subselects even more important.

Michael Isvy
November 4, 2019, 4:11 AM

Hello,
I was trying to find a solution that would avoid using:

That's because I don't always want my relationship to be eager. I would like to use `join fetch` in my JPQL query when I need the relationship to be eager.

I have a workaround which is as below:

One of my collections has to be a Set (because a Set is not a Bag). Is it the proper way to work with it? Also, I don't understand why moving to a Set is fixing the issue. Any explanation would be welcome .

Thanks!

Assignee

Steve Ebersole

Reporter

Steve Ebersole

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Priority

Major
Configure