|
| 1 | +package org.hibernate.orm.test.embeddable; |
| 2 | + |
| 3 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 4 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 5 | +import org.hibernate.testing.orm.junit.Jpa; |
| 6 | +import org.junit.jupiter.api.AfterEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import jakarta.persistence.Embeddable; |
| 10 | +import jakarta.persistence.Embedded; |
| 11 | +import jakarta.persistence.Entity; |
| 12 | +import jakarta.persistence.Id; |
| 13 | + |
| 14 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 15 | + |
| 16 | +@Jpa( |
| 17 | + annotatedClasses = { |
| 18 | + NullEmbeddableTest.EntityA.class |
| 19 | + } |
| 20 | +) |
| 21 | +@JiraKey("HHH-17290") |
| 22 | +public class NullEmbeddableTest { |
| 23 | + |
| 24 | + @AfterEach |
| 25 | + public void tearDown(EntityManagerFactoryScope scope) { |
| 26 | + scope.inTransaction( |
| 27 | + entityManager -> { |
| 28 | + entityManager.createQuery( "delete from EntityA" ).executeUpdate(); |
| 29 | + } |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testPersist(EntityManagerFactoryScope scope) { |
| 35 | + scope.inTransaction( |
| 36 | + entityManager -> { |
| 37 | + EntityA entityA = new EntityA( "1", null ); |
| 38 | + entityManager.persist( entityA ); |
| 39 | + } |
| 40 | + ); |
| 41 | + |
| 42 | + scope.inTransaction( |
| 43 | + entityManager -> { |
| 44 | + EntityA entityA = entityManager.find( EntityA.class, "1" ); |
| 45 | + assertThat( entityA.materialCost ).isNull(); |
| 46 | + } |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testUpdate(EntityManagerFactoryScope scope) { |
| 52 | + scope.inTransaction( |
| 53 | + entityManager -> { |
| 54 | + EntityA entityA = new EntityA( "1", null ); |
| 55 | + entityManager.persist( entityA ); |
| 56 | + } |
| 57 | + ); |
| 58 | + |
| 59 | + scope.inTransaction( |
| 60 | + entityManager -> { |
| 61 | + EntityA entityA = entityManager.find( EntityA.class, "1" ); |
| 62 | + entityA.materialCost = null; |
| 63 | + } |
| 64 | + ); |
| 65 | + |
| 66 | + scope.inTransaction( |
| 67 | + entityManager -> { |
| 68 | + EntityA entityA = entityManager.find( EntityA.class, "1" ); |
| 69 | + assertThat( entityA.materialCost ).isNull(); |
| 70 | + } |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testMerge(EntityManagerFactoryScope scope) { |
| 76 | + scope.inTransaction( |
| 77 | + entityManager -> { |
| 78 | + EntityA entityA = new EntityA( "1", new Cost( 2, "USD" ) ); |
| 79 | + entityManager.persist( entityA ); |
| 80 | + } |
| 81 | + ); |
| 82 | + |
| 83 | + scope.inTransaction( |
| 84 | + entityManager -> { |
| 85 | + EntityA entityA = new EntityA( "1", null ); |
| 86 | + entityManager.merge( entityA ); |
| 87 | + } |
| 88 | + ); |
| 89 | + |
| 90 | + scope.inTransaction( |
| 91 | + entityManager -> { |
| 92 | + EntityA entityA = entityManager.find( EntityA.class, "1" ); |
| 93 | + assertThat( entityA.materialCost ).isNull(); |
| 94 | + } |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + @Entity(name = "EntityA") |
| 99 | + public static class EntityA { |
| 100 | + @Id |
| 101 | + String id; |
| 102 | + |
| 103 | + @Embedded |
| 104 | + Cost materialCost; |
| 105 | + |
| 106 | + public EntityA() { |
| 107 | + } |
| 108 | + |
| 109 | + public EntityA(String id, Cost materialCost) { |
| 110 | + this.id = id; |
| 111 | + this.materialCost = materialCost; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Embeddable |
| 116 | + public static class Cost { |
| 117 | + int amount; |
| 118 | + String currency; |
| 119 | + |
| 120 | + public Cost() { |
| 121 | + } |
| 122 | + |
| 123 | + public Cost(Integer amount, String currency) { |
| 124 | + this.amount = amount; |
| 125 | + this.currency = currency; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments