multiLoad behavior

Description

When loading multiple entities via:

the resulting list may have unexpected properties:

  1. If the fetch for SomeClass contains left outer joins, some instances may be contained multiple times in the resulting list.

  2. Both ids and the returned List<> are ordered collections. The principle of least surprise dictates, that both collections have the same order. In pseudo-code: list.get(j).getId().equals(ids.get(j)) should be true for any j. Unless of course a different behavior is documented (it's not, as far as I can tell)

  3. If an entity for a given id cannot be found, I expect a null in the returned list at the index of that id. That's not the case.

It would be nice, if either these failures got fixed or the documentation got adjusted to reflect the behavior.

Thanks.

(this is related to HHH-7572)

Activity

Show:

Former user August 18, 2017 at 6:28 AM

Fixed in 5.1 branch as well.

Steve Ebersole July 26, 2016 at 4:23 PM

I implemented the fixes for this issue and together. What I ended up doing is:

  1. Add new MultiIdentifierLoadAccess#enableReturnOfDeletedEntities option (default == false)

  2. Add new MultiIdentifierLoadAccess#enableOrderedReturn option (default == true)

  3. Added Javadoc clarification to MultiIdentifierLoadAccess#multiLoad to see the Javadocs for the above 2 options.

I am not super stoked about the way the ordering happens, at the moment. For now I essentially:

  1. Perform X number of batch loads

  2. After all the batch loads, come back and build the result List based on the incoming id positions.

I did it this way to also handle de-duplication which is the bug referenced in the first point here.

Hendrik Schreiber July 26, 2016 at 7:33 AM

Regarding 2. (ordered Lists as input and output types):

In lieu of javadocs stating something else, the method signature is the documentation.
Therefore the documentation says in this case:

Hey there... I accept objects in a certain order. If the order didn't matter, my author would have used the type Collection and not List. Also: I'm returning objects in a certain order. Again, otherwise my author would have used Collection instead of List.

Based on the method signature, these are reasonable assumptions about what the method does. After all, it's a Java method, not an SQL call. With my mind set on the underlying SQL implementation—yes, perhaps I would assume other behavior. But since Hibernate is an ORM, I side with typical behavior for Java (cause that's what it's all about).

Now, you are absolutely right, which order is chosen when object are returned, i.e. the principle that guides sorting in this case, is completely unspecified. Perhaps that's what you mean with "leap of logic" .

But let's not argue how APIs need to be interpreted. That's fun, but rarely leads to something.

All I'm really saying is, that I was surprised by what the method does. I had expected/wished for something else. That's why I filed the JIRA issue. Part 1 is most definitely a bug. But with 2 and 3, it's probably a matter of opinion, as you already stated. I'm not even sure if other people have use cases that require this kind of processing (I do though). Also, I don't have stakes in Hibernate as a developer, just as a consumer. As a consumer, I'd appreciate fixing of 1. and clarification of 2. I.e. either define the order of both lists somewhere, change the type to `Collection`, introduce a new method, ... there are a bunch of possibilities. Regarding 3—that would be nice and very Java-like in my eyes. But perhaps also quite unexpected for other people.

And yes, the behavior I suggest requires additional processing and therefore has a performance impact. With that in mind, a flag that allows turning this behavior on and off is probably a good idea. Or a second method. I don't have any strong opinions on this.

To summarize:

  • Fix the bug in 1.

  • Clarify the behavior somehow, via different types or javadocs.

  • (Optionally) Add another behavior that is more like what I wished for and document what it does.

In any case, I appreciate you taking the time to work on this.

Hendrik Schreiber July 25, 2016 at 6:31 PM

Thanks for inviting me to discuss. I won't be able to do so tonight, but will gladly argue my point tomorrow morning (central European time).

Steve Ebersole July 25, 2016 at 5:57 PM

You could also come chat about it in our HipChat channel @ https://www.hipchat.com/goMzVNYC9

Fixed

Details

Assignee

Reporter

Fix versions

Affects versions

Priority

Created March 15, 2016 at 10:53 AM
Updated August 18, 2017 at 6:29 AM
Resolved August 18, 2017 at 6:28 AM