Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions hibernate-core/src/main/java/org/hibernate/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -706,18 +706,21 @@ public interface Session extends SharedSessionContract, EntityManager {
/**
* Copy the state of the given object onto the persistent object with the same
* identifier. If there is no persistent instance currently associated with
* the session, it will be loaded. Return the persistent instance. If the
* given instance is unsaved, save a copy and return it as a newly persistent
* instance. The given instance does not become associated with the session.
* This operation cascades to associated instances if the association is mapped
* with {@link jakarta.persistence.CascadeType#MERGE}.
* the session, it is loaded using the given {@link EntityGraph}, which is
* interpreted as a load graph. Return the persistent instance. If the given
* instance is unsaved, save a copy and return it as a newly persistent instance.
* The given instance does not become associated with the session. This operation
* cascades to associated instances if the association is mapped with
* {@link jakarta.persistence.CascadeType#MERGE}.
*
* @param object a detached instance with state to be copied
* @param loadGraph entity graph interpreted as a load graph
* @param loadGraph an entity graph interpreted as a load graph
*
* @return an updated persistent instance
*
* @since 7.0
*/
<T> T merge( T object, EntityGraph<?> loadGraph);
<T> T merge(T object, EntityGraph<? super T> loadGraph);

/**
* Make a transient instance persistent and mark it for later insertion in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ public <T> T merge(String entityName, T object) {
}

@Override
public <T> T merge(T object, EntityGraph<?> loadGraph) {
public <T> T merge(T object, EntityGraph<? super T> loadGraph) {
return delegate.merge( object, loadGraph );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public <T> T merge(String entityName, T object) {
}

@Override
public <T> T merge(T object, EntityGraph<?> loadGraph) {
public <T> T merge(T object, EntityGraph<? super T> loadGraph) {
return this.lazySession.get().merge( object, loadGraph );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ public <T> T merge(String entityName, T object) {
}

@Override
public <T> T merge(T object, EntityGraph<?> loadGraph) {
public <T> T merge(T object, EntityGraph<? super T> loadGraph) {
final var effectiveEntityGraph = loadQueryInfluencers.getEffectiveEntityGraph();
try {
effectiveEntityGraph.applyGraph( (RootGraphImplementor<?>) loadGraph, GraphSemantic.LOAD );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.hibernate.jpa.SpecHints.HINT_SPEC_LOAD_GRAPH;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -58,21 +56,11 @@ public static void init(EntityManagerFactoryScope scope) {
@Test
public void testGrandChildHasNotBeenInitializedByMerge(EntityManagerFactoryScope scope) {
Parent parent = scope.fromTransaction( entityManager ->
entityManager.find(
Parent.class,
PARENT_ID_1,
Collections.singletonMap(
HINT_SPEC_LOAD_GRAPH,
entityManager.getEntityGraph( "parent.child" ) ) )
entityManager.find( LoadGraphMergeTest_.Parent_._parent_child, PARENT_ID_1 )
);

Parent parent2 = scope.fromTransaction( entityManager ->
entityManager.find(
Parent.class,
PARENT_ID_2,
Collections.singletonMap(
HINT_SPEC_LOAD_GRAPH,
entityManager.getEntityGraph( "parent.child" ) ) )
entityManager.find( LoadGraphMergeTest_.Parent_._parent_child, PARENT_ID_2)
);

scope.inTransaction( entityManager -> {
Expand All @@ -81,8 +69,7 @@ public void testGrandChildHasNotBeenInitializedByMerge(EntityManagerFactoryScope

Session session = entityManager.unwrap( Session.class );

Parent mergedParent = session.merge( parent,
entityManager.getEntityGraph( "parent.child" ) );
Parent mergedParent = session.merge( parent, LoadGraphMergeTest_.Parent_._parent_child );

Child child = mergedParent.getChild();
assertTrue( Hibernate.isInitialized( child ) );
Expand Down Expand Up @@ -113,7 +100,7 @@ public void testChildHasNotBeenInitializedByMerge(EntityManagerFactoryScope scop
assertFalse( Hibernate.isInitialized( child1 ) );

Session session = entityManager.unwrap( Session.class );
Parent mergedParent = session.merge( parent, session.createEntityGraph( "parent" ) );
Parent mergedParent = session.merge( parent, LoadGraphMergeTest_.Parent_._parent );

Child child = mergedParent.getChild();
assertFalse( Hibernate.isInitialized( child ),
Expand Down