From 734bd775bfabdf2dac9c1fffc080df180b829cb5 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Wed, 7 Feb 2024 10:39:56 +0100 Subject: [PATCH] HHH-17695 Add test for issue --- .../embeddable/NestedJsonEmbeddableTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/NestedJsonEmbeddableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/NestedJsonEmbeddableTest.java index 90e9b34cea..fb46cb55b8 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/NestedJsonEmbeddableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/embeddable/NestedJsonEmbeddableTest.java @@ -28,6 +28,7 @@ import org.hibernate.testing.orm.domain.gambit.EntityOfBasics; import org.hibernate.testing.orm.domain.gambit.MutableValue; import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest; import org.hibernate.testing.orm.junit.DialectFeatureChecks; +import org.hibernate.testing.orm.junit.Jira; import org.hibernate.testing.orm.junit.RequiresDialectFeature; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -230,6 +231,68 @@ public class NestedJsonEmbeddableTest extends BaseSessionFactoryFunctionalTest { ); } + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-17695" ) + public void testNullNestedAggregate() { + sessionFactoryScope().inTransaction( + entityManager -> { + JsonHolder jsonHolder = new JsonHolder(3L, "abc", 30, "String 'xyz'", null ); + entityManager.persist( jsonHolder ); + } + ); + sessionFactoryScope().inTransaction( + entityManager -> { + JsonHolder jsonHolder = entityManager.createQuery( "from JsonHolder b where b.id = 3", JsonHolder.class ).getSingleResult(); + assertEquals( "abc", jsonHolder.theJson.stringField ); + assertEquals( 30, jsonHolder.theJson.simpleEmbeddable.integerField ); + assertEquals( "String 'xyz'", jsonHolder.theJson.simpleEmbeddable.doubleNested.theNested.theLeaf.stringField ); + assertNull( jsonHolder.getAggregate() ); + } + ); + } + + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-17695" ) + public void testNullNestedEmbeddable() { + sessionFactoryScope().inTransaction( + entityManager -> { + JsonHolder jsonHolder = new JsonHolder( ); + jsonHolder.setId( 3L ); + jsonHolder.setTheJson( new TheJson( "abc", null, EmbeddableAggregate.createAggregate1() ) ); + entityManager.persist( jsonHolder ); + } + ); + sessionFactoryScope().inTransaction( + entityManager -> { + JsonHolder jsonHolder = entityManager.createQuery( "from JsonHolder b where b.id = 3", JsonHolder.class ).getSingleResult(); + assertEquals( "abc", jsonHolder.theJson.stringField ); + assertNull( jsonHolder.theJson.simpleEmbeddable ); + assertStructEquals( EmbeddableAggregate.createAggregate1(), jsonHolder.getAggregate() ); + } + ); + } + + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-17695" ) + public void testNullNestedEmbeddableAndAggregate() { + sessionFactoryScope().inTransaction( + entityManager -> { + JsonHolder jsonHolder = new JsonHolder( ); + jsonHolder.setId( 3L ); + jsonHolder.setTheJson( new TheJson( "abc", null, null ) ); + entityManager.persist( jsonHolder ); + } + ); + sessionFactoryScope().inTransaction( + entityManager -> { + JsonHolder jsonHolder = entityManager.createQuery( "from JsonHolder b where b.id = 3", JsonHolder.class ).getSingleResult(); + assertEquals( "abc", jsonHolder.theJson.stringField ); + assertNull( jsonHolder.theJson.simpleEmbeddable ); + assertNull( jsonHolder.getAggregate() ); + } + ); + } + @Test @RequiresDialectFeature(feature = DialectFeatureChecks.SupportsJsonComponentUpdate.class) public void testUpdateAggregateMember() { @@ -393,6 +456,12 @@ public class NestedJsonEmbeddableTest extends BaseSessionFactoryFunctionalTest { this.simpleEmbeddable = new SimpleEmbeddable( integerField, leaf ); this.nested = nested; } + + public TheJson(String stringField, SimpleEmbeddable simpleEmbeddable, EmbeddableAggregate nested) { + this.stringField = stringField; + this.simpleEmbeddable = simpleEmbeddable; + this.nested = nested; + } } @Embeddable