Skip to content

Commit 302d690

Browse files
dreab8sebersole
authored andcommitted
HHH-15271 Don't initialize lazy associations via merge
1 parent 26e41b9 commit 302d690

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

hibernate-core/src/main/java/org/hibernate/Session.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,22 @@ public interface Session extends SharedSessionContract, EntityManager {
660660
*/
661661
<T> T merge(String entityName, T object);
662662

663+
/**
664+
* Copy the state of the given object onto the persistent object with the same
665+
* identifier. If there is no persistent instance currently associated with
666+
* the session, it will be loaded. Return the persistent instance. If the
667+
* given instance is unsaved, save a copy and return it as a newly persistent
668+
* instance. The given instance does not become associated with the session.
669+
* This operation cascades to associated instances if the association is mapped
670+
* with {@link jakarta.persistence.CascadeType#MERGE}.
671+
*
672+
* @param object a detached instance with state to be copied
673+
* @param loadGraph entity graph interpreted as a load graph
674+
*
675+
* @return an updated persistent instance
676+
*/
677+
<T> T merge( T object, EntityGraph<?> loadGraph);
678+
663679
/**
664680
* Make a transient instance persistent and mark it for later insertion in the
665681
* database. This operation cascades to associated instances if the association

hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,11 @@ public <T> T merge(String entityName, T object) {
825825
return delegate.merge( entityName, object );
826826
}
827827

828+
@Override
829+
public <T> T merge(T object, EntityGraph<?> loadGraph) {
830+
return delegate.merge( object, loadGraph );
831+
}
832+
828833
@Override
829834
public void persist(Object object) {
830835
delegate.persist( object );

hibernate-core/src/main/java/org/hibernate/engine/spi/SessionLazyDelegator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ public <T> T merge(String entityName, T object) {
210210
return this.lazySession.get().merge( entityName, object );
211211
}
212212

213+
@Override
214+
public <T> T merge(T object, EntityGraph<?> loadGraph) {
215+
return this.lazySession.get().merge( object, loadGraph );
216+
}
217+
213218
@Override
214219
public void persist(Object object) {
215220
this.lazySession.get().persist( object );

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,19 @@ public <T> T merge(String entityName, T object) {
803803
return (T) fireMerge( new MergeEvent( entityName, object, this ) );
804804
}
805805

806+
@Override
807+
public <T> T merge(T object, EntityGraph<?> loadGraph) {
808+
EffectiveEntityGraph effectiveEntityGraph = loadQueryInfluencers.getEffectiveEntityGraph();
809+
try {
810+
effectiveEntityGraph
811+
.applyGraph( (RootGraphImplementor<?>) loadGraph, GraphSemantic.LOAD );
812+
return merge( object );
813+
}
814+
finally {
815+
effectiveEntityGraph.clear();
816+
}
817+
}
818+
806819
@Override @SuppressWarnings("unchecked")
807820
public <T> T merge(T object) {
808821
checkOpen();

0 commit comments

Comments
 (0)