Skip to content

Merge and persist with reference #1498

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 @@ -11,7 +11,7 @@
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

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

import jakarta.persistence.CascadeType;
Expand All @@ -22,24 +22,21 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
* @see LazyUniqueKeyTest
*/
public class EagerUniqueKeyTest extends BaseReactiveTest {

@Override
protected Collection<Class<?>> annotatedEntities() {
return List.of( Foo.class, Bar.class );
}

@After
public void cleanDb(TestContext context) {
test( context, getSessionFactory()
.withTransaction( s -> s.createQuery( "delete from Foo" ).executeUpdate()
.thenCompose( v -> s.createQuery( "delete from Bar" ).executeUpdate() ) ) );
}

@Test
public void testFindJoin(TestContext context) {
Foo foo = new Foo( new Bar( "unique" ) );
Expand All @@ -65,20 +62,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 +105,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 +139,8 @@ static class Bar implements Serializable {
Bar() {
}

@GeneratedValue
@Id
@GeneratedValue
long id;
@Column(name = "nat_key", unique = true)
String key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

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

import jakarta.persistence.CascadeType;
Expand All @@ -24,6 +25,9 @@
import java.util.Collection;
import java.util.List;

/**
* @see EagerUniqueKeyTest
*/
public class LazyUniqueKeyTest extends BaseReactiveTest {

@Override
Expand Down Expand Up @@ -62,6 +66,7 @@ public void testMergeDetached(TestContext context) {
) ) );
}

@Ignore // This also fails in ORM
@Test
public void testMergeReference(TestContext context) {
Bar bar = new Bar( "unique3" );
Expand All @@ -76,6 +81,23 @@ public void testMergeReference(TestContext context) {
) ) );
}

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

@Entity(name = "Foo")
static class Foo {
Foo(Bar bar) {
Expand Down Expand Up @@ -108,5 +130,13 @@ static class Bar implements Serializable {
long id;
@Column(name = "nat_key", unique = true)
String key;

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}
}
}