HHH-17761 Add a reproducer

This commit is contained in:
marko-bekhta 2024-02-20 13:59:59 +01:00 committed by Steve Ebersole
parent 4893b6d3ab
commit 12c01df281
1 changed files with 63 additions and 1 deletions

View File

@ -25,6 +25,7 @@ import jakarta.persistence.Table;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.fail;
@ -37,7 +38,7 @@ public class MergeEnhancedEntityTest extends BaseCoreFunctionalTestCase {
private Person person;
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[]{Person.class, PersonAddress.class};
return new Class<?>[]{Person.class, PersonAddress.class, NullablePerson.class};
}
@Before
@ -77,11 +78,49 @@ public class MergeEnhancedEntityTest extends BaseCoreFunctionalTestCase {
} );
}
@Test
public void testMergeWithNullValues() {
doInHibernate( this::sessionFactory, em -> {
NullablePerson nullablePerson = new NullablePerson( 1L, "Sam", 100 );
em.persist( nullablePerson );
} );
doInHibernate( this::sessionFactory, em -> {
NullablePerson updated = em.find( NullablePerson.class, 1L );
assertThat( updated.name ).isEqualTo( "Sam" );
assertThat( updated.number ).isEqualTo( 100 );
} );
// only some properties are null
doInHibernate( this::sessionFactory, em -> {
NullablePerson nullablePerson = new NullablePerson( 1L, "Joe", null );
em.merge( nullablePerson );
} );
doInHibernate( this::sessionFactory, em -> {
NullablePerson updated = em.find( NullablePerson.class, 1L );
assertThat( updated.name ).isEqualTo( "Joe" );
assertThat( updated.number ).isNull();
} );
// all properties are null:
doInHibernate( this::sessionFactory, em -> {
NullablePerson nullablePerson = new NullablePerson( 1L, null, null );
em.merge( nullablePerson );
} );
doInHibernate( this::sessionFactory, em -> {
NullablePerson updated = em.find( NullablePerson.class, 1L );
assertThat( updated.name ).isNull();
assertThat( updated.number ).isNull();
} );
}
@After
public void cleanup() {
doInHibernate( this::sessionFactory, s -> {
s.delete( person );
} );
doInHibernate( this::sessionFactory, s -> {
s.createQuery( "delete from NullablePerson" );
} );
}
// --- //
@ -118,4 +157,27 @@ public class MergeEnhancedEntityTest extends BaseCoreFunctionalTestCase {
@ManyToOne( optional = false, fetch = FetchType.LAZY )
Person parent;
}
@Entity(name = "NullablePerson")
@Table(name = "NULLABLE_PERSON")
private static class NullablePerson {
@Id
Long id;
@Column
String name;
@Column
Integer number;
NullablePerson() {
}
NullablePerson(Long id, String name, Integer number) {
this.id = id;
this.name = name;
this.number = number;
}
}
}