I was looking through the source code of Hibernate 5.6 and found an interesting detail. In the DefaultMergeEventListener class the copyValues
method is called during the entityIsPersistent
method invocation.
It's odd that entity
and target
parameters are equal to the same entity that is already persisted. Therefore, Hibernate copies the entity's attributes to the same memory spot it was copied from. Seems like that operation just burns CPU cycles and does no benefit.
Is that copying necessary, if we know that the passed entity is already persisted? BTW, the latest change in this line of code was on 23 Nov 2013 (and it was the checkstyle cleanup). Maybe it's just legacy stuff that should be removed?
P.S. I posted this discussion originally in Hibernate repository itself.
CodePudding user response:
The method you mentioned calls TypeHelper#replace() that does not do a simple copy of entity
into target
but it executes a replacement.