|
| 1 | +package org.hibernate.orm.test.embeddable; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 7 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 8 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 9 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 10 | +import org.junit.jupiter.api.BeforeAll; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import jakarta.persistence.Column; |
| 14 | +import jakarta.persistence.ElementCollection; |
| 15 | +import jakarta.persistence.Embeddable; |
| 16 | +import jakarta.persistence.Entity; |
| 17 | +import jakarta.persistence.FetchType; |
| 18 | +import jakarta.persistence.Id; |
| 19 | + |
| 20 | +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; |
| 21 | + |
| 22 | +@DomainModel( |
| 23 | + annotatedClasses = { |
| 24 | + ElementCollectionAllPrimitivePropertyUpdateTest.MyEntity.class |
| 25 | + } |
| 26 | +) |
| 27 | +@SessionFactory |
| 28 | +@JiraKey( "HHH-17257" ) |
| 29 | +public class ElementCollectionAllPrimitivePropertyUpdateTest { |
| 30 | + |
| 31 | + private static final Long ENTITY_ID = 1l; |
| 32 | + |
| 33 | + @BeforeAll |
| 34 | + public void setUp(SessionFactoryScope scope) { |
| 35 | + scope.inTransaction( |
| 36 | + session -> { |
| 37 | + MyEntity myEntity = new MyEntity( ENTITY_ID, Set.of( |
| 38 | + new MyEmbeddable( 1, false ), |
| 39 | + new MyEmbeddable( 2, false ) |
| 40 | + ) ); |
| 41 | + session.persist( myEntity ); |
| 42 | + |
| 43 | + } |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testUpdateElement(SessionFactoryScope scope) { |
| 49 | + |
| 50 | + scope.inTransaction( |
| 51 | + session -> { |
| 52 | + MyEntity myEntity = session.find( MyEntity.class, ENTITY_ID ); |
| 53 | + Set<MyEmbeddable> elementCollection = myEntity.getElementCollection(); |
| 54 | + assertThat( elementCollection ).hasSize( 2 ); |
| 55 | + |
| 56 | + elementCollection.stream().filter( it -> it.getIntField() == 1 ).forEach( |
| 57 | + it -> it.setBooleanField( true ) ); |
| 58 | + } |
| 59 | + ); |
| 60 | + |
| 61 | + scope.inTransaction( |
| 62 | + session -> { |
| 63 | + MyEntity myEntity = session.find( MyEntity.class, ENTITY_ID ); |
| 64 | + assertThat( myEntity.getElementCollection() ).hasSize( 2 ); |
| 65 | + assertThat( myEntity.getElementCollection() ).extracting( MyEmbeddable::isBooleanField ) |
| 66 | + .containsExactlyInAnyOrder( true, false ); |
| 67 | + } |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + @Entity(name = "MyEntity") |
| 73 | + public static class MyEntity { |
| 74 | + @Id |
| 75 | + private Long id; |
| 76 | + |
| 77 | + private String name; |
| 78 | + |
| 79 | + @ElementCollection(fetch = FetchType.EAGER) |
| 80 | + private Set<MyEmbeddable> elementCollection; |
| 81 | + |
| 82 | + public MyEntity() { |
| 83 | + } |
| 84 | + |
| 85 | + public MyEntity(Long id, Set<MyEmbeddable> elementCollection) { |
| 86 | + this.id = id; |
| 87 | + this.elementCollection = elementCollection; |
| 88 | + } |
| 89 | + |
| 90 | + public Long getId() { |
| 91 | + return id; |
| 92 | + } |
| 93 | + |
| 94 | + public Set<MyEmbeddable> getElementCollection() { |
| 95 | + return elementCollection; |
| 96 | + } |
| 97 | + |
| 98 | + public void setElementCollection(Set<MyEmbeddable> elementCollection) { |
| 99 | + this.elementCollection = elementCollection; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Embeddable |
| 104 | + public static class MyEmbeddable { |
| 105 | + @Column |
| 106 | + // the field is considered non-optional because of its primitive type |
| 107 | + private int intField; |
| 108 | + |
| 109 | + @Column |
| 110 | + // the field is considered non-optional because of its primitive type |
| 111 | + private boolean booleanField; |
| 112 | + |
| 113 | + public MyEmbeddable() { |
| 114 | + } |
| 115 | + |
| 116 | + public MyEmbeddable(int intField, boolean booleanField) { |
| 117 | + this.intField = intField; |
| 118 | + this.booleanField = booleanField; |
| 119 | + } |
| 120 | + |
| 121 | + public int getIntField() { |
| 122 | + return intField; |
| 123 | + } |
| 124 | + |
| 125 | + public void setIntField(int intField) { |
| 126 | + this.intField = intField; |
| 127 | + } |
| 128 | + |
| 129 | + public boolean isBooleanField() { |
| 130 | + return booleanField; |
| 131 | + } |
| 132 | + |
| 133 | + public void setBooleanField(boolean booleanField) { |
| 134 | + this.booleanField = booleanField; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public boolean equals(Object o) { |
| 139 | + if ( this == o ) { |
| 140 | + return true; |
| 141 | + } |
| 142 | + if ( o == null || getClass() != o.getClass() ) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + MyEmbeddable that = (MyEmbeddable) o; |
| 146 | + return booleanField == that.booleanField && Objects.equals( intField, that.intField ); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public int hashCode() { |
| 151 | + return Objects.hash( intField, booleanField ); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments