|
| 1 | +package org.hibernate.orm.test.annotations.secondarytable; |
| 2 | + |
| 3 | +import org.hibernate.annotations.DynamicUpdate; |
| 4 | + |
| 5 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 6 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 7 | +import org.hibernate.testing.orm.junit.Jpa; |
| 8 | +import org.junit.jupiter.api.BeforeAll; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import jakarta.persistence.Column; |
| 12 | +import jakarta.persistence.Entity; |
| 13 | +import jakarta.persistence.Id; |
| 14 | +import jakarta.persistence.PrimaryKeyJoinColumn; |
| 15 | +import jakarta.persistence.SecondaryTable; |
| 16 | + |
| 17 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 18 | + |
| 19 | +@Jpa( |
| 20 | + annotatedClasses = { |
| 21 | + SecondaryTableDynamicUpateTest.TestEntity.class |
| 22 | + } |
| 23 | +) |
| 24 | +@JiraKey("HHH-17587") |
| 25 | +public class SecondaryTableDynamicUpateTest { |
| 26 | + |
| 27 | + private static final Long ENTITY_ID = 123l; |
| 28 | + private static final String COL_VALUE = "col"; |
| 29 | + private static final String COL1_VALUE = "col1"; |
| 30 | + private static final String COL2_VALUE = "col2"; |
| 31 | + |
| 32 | + @BeforeAll |
| 33 | + public static void setUp(EntityManagerFactoryScope scope) { |
| 34 | + scope.inTransaction( |
| 35 | + entityManager -> { |
| 36 | + TestEntity testEntity = new TestEntity( ENTITY_ID, COL_VALUE, COL1_VALUE, COL2_VALUE ); |
| 37 | + entityManager.persist( testEntity ); |
| 38 | + } |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testSetSecondaryTableColumnToNull(EntityManagerFactoryScope scope) { |
| 44 | + scope.inTransaction( |
| 45 | + entityManager -> { |
| 46 | + TestEntity testEntity = entityManager.find( TestEntity.class, ENTITY_ID ); |
| 47 | + assertThat( testEntity.getTestCol() ).isEqualTo( COL_VALUE ); |
| 48 | + assertThat( testEntity.getTestCol1() ).isEqualTo( COL1_VALUE ); |
| 49 | + assertThat( testEntity.getTestCol2() ).isEqualTo( COL2_VALUE ); |
| 50 | + testEntity.setTestCol1( null ); |
| 51 | + } |
| 52 | + ); |
| 53 | + |
| 54 | + scope.inTransaction( |
| 55 | + entityManager -> { |
| 56 | + TestEntity testEntity = entityManager.find( TestEntity.class, ENTITY_ID ); |
| 57 | + assertThat( testEntity ).isNotNull(); |
| 58 | + assertThat( testEntity.getTestCol() ).isEqualTo( COL_VALUE ); |
| 59 | + assertThat( testEntity.getTestCol1() ).isNull(); |
| 60 | + assertThat( testEntity.getTestCol2() ).isEqualTo( COL2_VALUE ); |
| 61 | + testEntity.setTestCol2( null ); |
| 62 | + } |
| 63 | + ); |
| 64 | + |
| 65 | + scope.inTransaction( |
| 66 | + entityManager -> { |
| 67 | + TestEntity testEntity = entityManager.find( TestEntity.class, ENTITY_ID ); |
| 68 | + assertThat( testEntity ).isNotNull(); |
| 69 | + assertThat( testEntity.getTestCol() ).isEqualTo( COL_VALUE ); |
| 70 | + assertThat( testEntity.getTestCol1() ).isNull(); |
| 71 | + assertThat( testEntity.getTestCol2() ).isNull(); |
| 72 | + testEntity.setTestCol1( COL1_VALUE ); |
| 73 | + testEntity.setTestCol( null ); |
| 74 | + } |
| 75 | + ); |
| 76 | + |
| 77 | + scope.inTransaction( |
| 78 | + entityManager -> { |
| 79 | + TestEntity testEntity = entityManager.find( TestEntity.class, ENTITY_ID ); |
| 80 | + assertThat( testEntity ).isNotNull(); |
| 81 | + assertThat( testEntity.getTestCol() ).isNull(); |
| 82 | + assertThat( testEntity.getTestCol1() ).isEqualTo( COL1_VALUE ); |
| 83 | + assertThat( testEntity.getTestCol2() ).isNull(); |
| 84 | + testEntity.setTestCol2( null ); |
| 85 | + } |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + @Entity(name = "TestEntity") |
| 90 | + @SecondaryTable(name = "SECOND_TABLE_TEST", pkJoinColumns = @PrimaryKeyJoinColumn(name = "ID")) |
| 91 | + @DynamicUpdate |
| 92 | + public static class TestEntity { |
| 93 | + |
| 94 | + @Id |
| 95 | + private Long id; |
| 96 | + |
| 97 | + @Column(name = "TEST_COL") |
| 98 | + private String testCol; |
| 99 | + |
| 100 | + @Column(name = "TESTCOL1", table = "SECOND_TABLE_TEST") |
| 101 | + private String testCol1; |
| 102 | + |
| 103 | + @Column(name = "TESTCOL2", table = "SECOND_TABLE_TEST") |
| 104 | + private String testCol2; |
| 105 | + |
| 106 | + public TestEntity() { |
| 107 | + } |
| 108 | + |
| 109 | + public TestEntity(Long id, String testCol, String testCol1, String testCol2) { |
| 110 | + this.id = id; |
| 111 | + this.testCol = testCol; |
| 112 | + this.testCol1 = testCol1; |
| 113 | + this.testCol2 = testCol2; |
| 114 | + } |
| 115 | + |
| 116 | + public Long getId() { |
| 117 | + return id; |
| 118 | + } |
| 119 | + |
| 120 | + public void setId(Long id) { |
| 121 | + this.id = id; |
| 122 | + } |
| 123 | + |
| 124 | + public String getTestCol() { |
| 125 | + return testCol; |
| 126 | + } |
| 127 | + |
| 128 | + public void setTestCol(String testCol) { |
| 129 | + this.testCol = testCol; |
| 130 | + } |
| 131 | + |
| 132 | + public String getTestCol1() { |
| 133 | + return testCol1; |
| 134 | + } |
| 135 | + |
| 136 | + public void setTestCol1(String testCol1) { |
| 137 | + this.testCol1 = testCol1; |
| 138 | + } |
| 139 | + |
| 140 | + public String getTestCol2() { |
| 141 | + return testCol2; |
| 142 | + } |
| 143 | + |
| 144 | + public void setTestCol2(String testCol2) { |
| 145 | + this.testCol2 = testCol2; |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | +} |
0 commit comments