Skip to content

Commit 74f0181

Browse files
dreab8beikov
authored andcommitted
HHH-17587 Add test for issue
1 parent c9805f2 commit 74f0181

File tree

2 files changed

+302
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)