ignoring collections on merge() #4398
Replies: 2 comments
-
I realized this is a bit harder than it looks at first blush. It's great for detached collections, but for attached collection it's a bit sorta nasty in the sense that it throws away unflushed changes. And for to-one associations we don't have a good reliable way to implement this, since we don't always have proxies in the object graph. So I guess there's good reasons why this method doesn't already exist. |
Beta Was this translation helpful? Give feedback.
-
This was done in H6. (There are new static methods on the |
Beta Was this translation helpful? Give feedback.
-
After much discussion surrounding this issue, I'm convinced that a very common need, that we simply don't really address at all today is the need to "trim" the object graph that is considered by
merge()
, based on transaction-specific considerations. (Think of it as almost like the inverse of the problem solved byEntityGraph
or fetch profiles.)Currently, you have some decent control over this via the
CascadeType
, but that has some problems:null
means an empty collection, not an uninitialized collection, so you can't just null out the collection when you want it to be ignored. This is especially uncomfortable for people who trim object graphs for serialization/deserialization.Note that none of these sources of discomfort is specific to unowned vs owned collections; the listed problems apply equally to both sorts of mapping.
Now, we've recently kicked around a few different ideas for this, and the ones which I think most directly attack the problem are:
merge()
that would let the program specify which collections to ignore, either via a callback, or by saying "ignorenull
s" or even in principle with something like anEntityGraph
.merge()
already ignores uninitialized collections.I had an inkling that 2 was already possible, just by
new
-ing aPersistentList
orPersistentSet
or whatever, and based on my initial testing that seems to be right. That is to say, it's already possible in Hibernate today, and has been for many years. Indeed, now that I think about it, I do seem to recall a user long ago telling me that they were doing that in their system. It's very likely that other users figured that out for themselves.On the other hand:
PersistentList
orPersistentSet
are clearly marked as internal APIs.Now, given that 2 is a way-lower-impact solution than 1, and works for people using the plain
EntityManager
API, and is also in some ways more flexible, I'm now inclined to think that it's really the path we should take at least for now. I propose:Hibernate
class that do nothing more thannew PersistentXxx()
in a blessed way.Hibernate.uninitialize()
to empty out the collection proxy.Now, this solution is perhaps slightly less comfortable for users than just setting a collection reference to
null
, but frankly it's far more consistent with the perfectly-reasonable semantics that Hibernate has had for many years, and has, as I say, a very low impact. And it avoids opening up any cans of worms with respect to the JPA spec.Note that I have not put much thought into the conceptually-similar problems affecting
@XxxToOne
associations, because the discomfort is much less acute there, and because I strongly suspect that the solution is entirely analogous: we could add a function a lot likegetReference()
to theHibernate
class, that gives a detached proxy for a class. I've been talking about collections above, but I think you could apply same reasoning to entity references.Beta Was this translation helpful? Give feedback.
All reactions