Skip to content

Merge reference #1497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -233,31 +233,17 @@ protected CompletionStage<Void> entityIsPersistent(MergeEvent event, MergeContex
}

protected CompletionStage<Void> entityIsTransient(MergeEvent event, MergeContext copyCache) {

LOG.trace( "Merging transient instance" );

final Object entity = event.getEntity();
final EventSource session = event.getSession();

final String entityName = event.getEntityName();
final EntityPersister persister = session.getEntityPersister( entityName, entity );

final Object id = persister.hasIdentifierProperty()
? persister.getIdentifier( entity, session )
: null;

final Object copy;
final Object existingCopy = copyCache.get( entity );
if ( existingCopy != null ) {
persister.setIdentifier( copyCache.get( entity ), id, session );
copy = existingCopy;
}
else {
copy = session.instantiate( persister, id );

//before cascade!
copyCache.put( entity, copy, true );
}
final Object copy = copyEntity( copyCache, entity, session, persister, id );

// cascade first, so that all unsaved objects get their
// copy created before we actually copy
Expand Down Expand Up @@ -286,6 +272,20 @@ protected CompletionStage<Void> entityIsTransient(MergeEvent event, MergeContext
});
}

private static Object copyEntity(MergeContext copyCache, Object entity, EventSource session, EntityPersister persister, Object id) {
final Object existingCopy = copyCache.get( entity );
if ( existingCopy != null ) {
persister.setIdentifier( copyCache.get( entity ), id, session );
return existingCopy;
}
else {
final Object copy = session.instantiate( persister, id );
//before cascade!
copyCache.put( entity, copy, true );
return copy;
}
}

private static class CollectionVisitor extends WrapVisitor {
CollectionVisitor(Object entity, Object id, EventSource session) {
super( entity, id, session );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.hibernate.annotations.FetchMode;

import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;

import jakarta.persistence.CascadeType;
Expand All @@ -22,6 +23,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -65,20 +67,38 @@ public void testMergeDetached(TestContext context) {
.withTransaction( session -> session.merge( new Foo( bar ) ) ) )
.thenCompose( result -> getSessionFactory()
.withTransaction( session -> session.fetch( result.getBar() )
.thenAccept( b -> context.assertEquals( "unique2", b.getKey() ) )
) ) );
.thenAccept( b -> context.assertEquals( "unique2", b.getKey() ) )
) ) );
}

@Ignore // This also fails in ORM
@Test
public void testMergeReference(TestContext context) {
Bar bar = new Bar( "unique3" );
test( context, getSessionFactory()
.withTransaction( session -> session.persist( bar ) )
.thenCompose( i -> getSessionFactory()
.withTransaction( session -> session.merge( new Foo( session.getReference( Bar.class, bar.id ) )) ) )
.withTransaction( session -> session
.merge( new Foo( session.getReference( Bar.class, bar.getId() ) ) ) ) )
.thenCompose( result -> getSessionFactory().withTransaction( session -> session.fetch( result.getBar() )
.thenAccept( b -> context.assertEquals( "unique3", b.getKey() ) )
) ) );
) )
);
}

@Test
public void testPersistWithReference(TestContext context) {
Bar bar = new Bar( "uniquePersist" );
test( context, getSessionFactory()
.withTransaction( session -> session.persist( bar ) )
.thenCompose( i -> getSessionFactory()
.withTransaction( session -> {
Foo foo = new Foo( session.getReference( Bar.class, bar.getId() ) );
return session.persist( foo ).thenApply( v -> foo );
} ) )
.thenCompose( result -> getSessionFactory().withTransaction( session -> session.fetch( result.getBar() ) ) )
.thenAccept( b -> context.assertEquals( "uniquePersist", b.getKey() ) )
);
}

@Entity(name = "Foo")
Expand All @@ -90,8 +110,8 @@ static class Foo {
Foo() {
}

@GeneratedValue
@Id
@GeneratedValue
long id;
@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
@Fetch(FetchMode.JOIN)
Expand Down Expand Up @@ -124,8 +144,8 @@ static class Bar implements Serializable {
Bar() {
}

@GeneratedValue
@Id
@GeneratedValue
long id;
@Column(name = "nat_key", unique = true)
String key;
Expand Down