From b95035c950ddd7c946f5b0928ce288a93e85e998 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 28 Sep 2023 09:50:46 +0200 Subject: [PATCH] HHH-17257 Add additional test --- ...ctionAllNonOptionalPropertyUpdateTest.java | 2 + ...lectionAllPrimitivePropertyUpdateTest.java | 155 ++++++++++++++++++ ...llectionNonOptionalPropertyUpdateTest.java | 2 + ...tCollectionOptionalPropertyUpdateTest.java | 2 + 4 files changed, 161 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionAllPrimitivePropertyUpdateTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionAllNonOptionalPropertyUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionAllNonOptionalPropertyUpdateTest.java index b2cecc4070..57a2923255 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionAllNonOptionalPropertyUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionAllNonOptionalPropertyUpdateTest.java @@ -74,6 +74,8 @@ public class ElementCollectionAllNonOptionalPropertyUpdateTest { @Id private Long id; + private String name; + @ElementCollection(fetch = FetchType.EAGER) private Set elementCollection; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionAllPrimitivePropertyUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionAllPrimitivePropertyUpdateTest.java new file mode 100644 index 0000000000..8fa572ac4b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionAllPrimitivePropertyUpdateTest.java @@ -0,0 +1,155 @@ +package org.hibernate.orm.test.embeddable; + +import java.util.Objects; +import java.util.Set; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Column; +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Embeddable; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +@DomainModel( + annotatedClasses = { + ElementCollectionAllPrimitivePropertyUpdateTest.MyEntity.class + } +) +@SessionFactory +@JiraKey( "HHH-17257" ) +public class ElementCollectionAllPrimitivePropertyUpdateTest { + + private static final Long ENTITY_ID = 1l; + + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + MyEntity myEntity = new MyEntity( ENTITY_ID, Set.of( + new MyEmbeddable( 1, false ), + new MyEmbeddable( 2, false ) + ) ); + session.persist( myEntity ); + + } + ); + } + + @Test + public void testUpdateElement(SessionFactoryScope scope) { + + scope.inTransaction( + session -> { + MyEntity myEntity = session.find( MyEntity.class, ENTITY_ID ); + Set elementCollection = myEntity.getElementCollection(); + assertThat( elementCollection ).hasSize( 2 ); + + elementCollection.stream().filter( it -> it.getIntField() == 1 ).forEach( + it -> it.setBooleanField( true ) ); + } + ); + + scope.inTransaction( + session -> { + MyEntity myEntity = session.find( MyEntity.class, ENTITY_ID ); + assertThat( myEntity.getElementCollection() ).hasSize( 2 ); + assertThat( myEntity.getElementCollection() ).extracting( MyEmbeddable::isBooleanField ) + .containsExactlyInAnyOrder( true, false ); + } + ); + } + + + @Entity(name = "MyEntity") + public static class MyEntity { + @Id + private Long id; + + private String name; + + @ElementCollection(fetch = FetchType.EAGER) + private Set elementCollection; + + public MyEntity() { + } + + public MyEntity(Long id, Set elementCollection) { + this.id = id; + this.elementCollection = elementCollection; + } + + public Long getId() { + return id; + } + + public Set getElementCollection() { + return elementCollection; + } + + public void setElementCollection(Set elementCollection) { + this.elementCollection = elementCollection; + } + } + + @Embeddable + public static class MyEmbeddable { + @Column + // the field is considered non-optional because of its primitive type + private int intField; + + @Column + // the field is considered non-optional because of its primitive type + private boolean booleanField; + + public MyEmbeddable() { + } + + public MyEmbeddable(int intField, boolean booleanField) { + this.intField = intField; + this.booleanField = booleanField; + } + + public int getIntField() { + return intField; + } + + public void setIntField(int intField) { + this.intField = intField; + } + + public boolean isBooleanField() { + return booleanField; + } + + public void setBooleanField(boolean booleanField) { + this.booleanField = booleanField; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + MyEmbeddable that = (MyEmbeddable) o; + return booleanField == that.booleanField && Objects.equals( intField, that.intField ); + } + + @Override + public int hashCode() { + return Objects.hash( intField, booleanField ); + } + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionNonOptionalPropertyUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionNonOptionalPropertyUpdateTest.java index c25a67f1b0..f85f8ce1a5 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionNonOptionalPropertyUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionNonOptionalPropertyUpdateTest.java @@ -74,6 +74,8 @@ public class ElementCollectionNonOptionalPropertyUpdateTest { @Id private Long id; + private String name; + @ElementCollection(fetch = FetchType.EAGER) private Set elementCollection; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionOptionalPropertyUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionOptionalPropertyUpdateTest.java index e1067f5a07..508da3d991 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionOptionalPropertyUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/ElementCollectionOptionalPropertyUpdateTest.java @@ -74,6 +74,8 @@ public class ElementCollectionOptionalPropertyUpdateTest { @Id private Long id; + private String name; + @ElementCollection(fetch = FetchType.EAGER) private Set elementCollection;