Skip to content

Commit 50a9102

Browse files
committed
HHH-16957 Add test for issue
1 parent 0bb48c1 commit 50a9102

File tree

6 files changed

+962
-0
lines changed

6 files changed

+962
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.embeddable.generated;
6+
7+
import jakarta.persistence.Embeddable;
8+
import jakarta.persistence.Embedded;
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.Id;
11+
import org.hibernate.annotations.Generated;
12+
import org.hibernate.testing.orm.junit.DomainModel;
13+
import org.hibernate.testing.orm.junit.JiraKey;
14+
import org.hibernate.testing.orm.junit.SessionFactory;
15+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
16+
import org.junit.jupiter.api.Test;
17+
18+
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
19+
20+
@DomainModel(
21+
annotatedClasses = {
22+
EmbeddableInsertTimeGeneratedPropertiesTest.TestEntity.class
23+
}
24+
)
25+
@SessionFactory
26+
@JiraKey("HHH-16957")
27+
public class EmbeddableInsertTimeGeneratedPropertiesTest {
28+
29+
@Test
30+
public void testGeneratedProperties(SessionFactoryScope scope) {
31+
Long id = 1L;
32+
String nonGeneratedPropertyValue = "non generated";
33+
String generatedButWritablePropertyValue = "generated but writable";
34+
scope.inTransaction(
35+
session -> {
36+
TestEntity testEntity = new TestEntity( id, "generated", generatedButWritablePropertyValue,
37+
nonGeneratedPropertyValue );
38+
session.persist( testEntity );
39+
}
40+
);
41+
42+
scope.inTransaction(
43+
session -> {
44+
TestEntity testEntity = session.find( TestEntity.class, id );
45+
assertThat( testEntity.anEmbeddable.nonGeneratedProperty )
46+
.isEqualTo( nonGeneratedPropertyValue );
47+
assertThat( testEntity.anEmbeddable.generatedProperty )
48+
.isNull();
49+
assertThat( testEntity.anEmbeddable.generatedButWritableProperty )
50+
.isEqualTo( generatedButWritablePropertyValue );
51+
}
52+
);
53+
}
54+
55+
@Entity(name = "TestEntity")
56+
public static class TestEntity {
57+
@Id
58+
private Long id;
59+
60+
@Embedded
61+
private AnEmbeddable anEmbeddable;
62+
63+
64+
public TestEntity() {
65+
}
66+
67+
public TestEntity(Long id, String nonGeneratedProperty) {
68+
this.id = id;
69+
this.anEmbeddable = new AnEmbeddable( nonGeneratedProperty );
70+
}
71+
72+
public TestEntity(Long id, String generatedButWritableProperty, String nonGeneratedProperty) {
73+
this.id = id;
74+
this.anEmbeddable = new AnEmbeddable( generatedButWritableProperty, nonGeneratedProperty );
75+
}
76+
77+
public TestEntity(Long id, String generatedProperty, String generatedButWritableProperty, String nonGeneratedProperty) {
78+
this.id = id;
79+
this.anEmbeddable = new AnEmbeddable( generatedProperty, generatedButWritableProperty,
80+
nonGeneratedProperty );
81+
}
82+
}
83+
84+
@Embeddable
85+
public static class AnEmbeddable {
86+
@Generated
87+
private String generatedProperty;
88+
89+
@Generated(writable = true)
90+
private String generatedButWritableProperty;
91+
92+
private String nonGeneratedProperty;
93+
94+
95+
public AnEmbeddable() {
96+
}
97+
98+
public AnEmbeddable(String nonGeneratedProperty) {
99+
this.nonGeneratedProperty = nonGeneratedProperty;
100+
}
101+
102+
public AnEmbeddable(String generatedButWritableProperty, String nonGeneratedProperty) {
103+
this.generatedButWritableProperty = generatedButWritableProperty;
104+
this.nonGeneratedProperty = nonGeneratedProperty;
105+
}
106+
107+
public AnEmbeddable(String generatedProperty, String generatedButWritableProperty, String nonGeneratedProperty) {
108+
this.generatedProperty = generatedProperty;
109+
this.generatedButWritableProperty = generatedButWritableProperty;
110+
this.nonGeneratedProperty = nonGeneratedProperty;
111+
}
112+
}
113+
114+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.embeddable.generated;
6+
7+
import jakarta.persistence.Embeddable;
8+
import jakarta.persistence.Embedded;
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.Id;
11+
import org.hibernate.annotations.Generated;
12+
import org.hibernate.testing.orm.junit.DomainModel;
13+
import org.hibernate.testing.orm.junit.JiraKey;
14+
import org.hibernate.testing.orm.junit.SessionFactory;
15+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
16+
import org.junit.jupiter.api.Test;
17+
18+
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
19+
20+
@DomainModel(
21+
annotatedClasses = {
22+
EmbeddableTest.TestEntity.class
23+
}
24+
)
25+
@SessionFactory
26+
@JiraKey("HHH-16957")
27+
public class EmbeddableTest {
28+
29+
@Test
30+
public void testEmbeddable(SessionFactoryScope scope) {
31+
Long id = 1L;
32+
String nonGeneratedPropertyValue = "non generated";
33+
scope.inTransaction(
34+
session -> {
35+
TestEntity testEntity = new TestEntity( id, nonGeneratedPropertyValue );
36+
session.persist( testEntity );
37+
}
38+
);
39+
40+
scope.inTransaction(
41+
session -> {
42+
TestEntity testEntity = session.find( TestEntity.class, id );
43+
assertThat( testEntity.anEmbeddable.nonGeneratedProperty ).isEqualTo( nonGeneratedPropertyValue );
44+
}
45+
);
46+
}
47+
48+
@Entity(name = "TestEntity")
49+
public static class TestEntity {
50+
@Id
51+
private Long id;
52+
53+
@Embedded
54+
private AnEmbeddable anEmbeddable;
55+
56+
57+
public TestEntity() {
58+
}
59+
60+
public TestEntity(Long id, String generatedProperty, String nonGeneratedProperty) {
61+
this.id = id;
62+
this.anEmbeddable = new AnEmbeddable( generatedProperty, nonGeneratedProperty );
63+
}
64+
65+
public TestEntity(Long id, String nonGeneratedProperty) {
66+
this.id = id;
67+
this.anEmbeddable = new AnEmbeddable( nonGeneratedProperty );
68+
}
69+
}
70+
71+
@Embeddable
72+
public static class AnEmbeddable {
73+
@Generated
74+
private String generatedProperty;
75+
76+
private String nonGeneratedProperty;
77+
78+
79+
public AnEmbeddable() {
80+
}
81+
82+
public AnEmbeddable(String generatedProperty, String nonGeneratedProperty) {
83+
this.generatedProperty = generatedProperty;
84+
this.nonGeneratedProperty = nonGeneratedProperty;
85+
}
86+
87+
public AnEmbeddable(String nonGeneratedProperty) {
88+
this.nonGeneratedProperty = nonGeneratedProperty;
89+
}
90+
}
91+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.embeddable.generated;
6+
7+
import jakarta.persistence.Embeddable;
8+
import jakarta.persistence.Embedded;
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.Id;
11+
import org.hibernate.annotations.Generated;
12+
import org.hibernate.generator.EventType;
13+
import org.hibernate.testing.orm.junit.DomainModel;
14+
import org.hibernate.testing.orm.junit.JiraKey;
15+
import org.hibernate.testing.orm.junit.SessionFactory;
16+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
17+
import org.junit.jupiter.api.AfterEach;
18+
import org.junit.jupiter.api.Test;
19+
20+
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
21+
22+
@DomainModel(
23+
annotatedClasses = {
24+
EmbeddableUpdateTimeGeneratedPropertiesTest.TestEntity.class
25+
}
26+
)
27+
@SessionFactory
28+
@JiraKey("HHH-16957")
29+
public class EmbeddableUpdateTimeGeneratedPropertiesTest {
30+
31+
@AfterEach
32+
public void tearDown(SessionFactoryScope sessionFactoryScope) {
33+
sessionFactoryScope.getSessionFactory().getSchemaManager().truncateMappedObjects();
34+
}
35+
36+
@Test
37+
public void testGeneratedProperties(SessionFactoryScope scope) {
38+
Long id = 1L;
39+
String nonGeneratedPropertyValue = "non generated";
40+
String generatedButWritablePropertyValue = "generated but writable";
41+
String generatedPropertyValue = "generated";
42+
43+
scope.inTransaction(
44+
session -> {
45+
TestEntity testEntity = new TestEntity( id, generatedPropertyValue,
46+
generatedButWritablePropertyValue,
47+
nonGeneratedPropertyValue );
48+
session.persist( testEntity );
49+
}
50+
);
51+
52+
scope.inTransaction(
53+
session -> {
54+
TestEntity testEntity = session.find( TestEntity.class, id );
55+
assertThat( testEntity.anEmbeddable.nonGeneratedProperty )
56+
.isEqualTo( nonGeneratedPropertyValue );
57+
assertThat( testEntity.anEmbeddable.generatedProperty )
58+
.isEqualTo( generatedPropertyValue );
59+
assertThat( testEntity.anEmbeddable.generatedButWritableProperty )
60+
.isEqualTo( generatedButWritablePropertyValue );
61+
}
62+
);
63+
}
64+
65+
@Test
66+
public void testGeneratedProperties2(SessionFactoryScope scope) {
67+
Long id = 1L;
68+
String nonGeneratedPropertyValue = "non generated";
69+
scope.inTransaction(
70+
session -> {
71+
TestEntity testEntity = new TestEntity( id, nonGeneratedPropertyValue );
72+
session.persist( testEntity );
73+
}
74+
);
75+
76+
String generatedButWritablePropertyValue = "generated but writable";
77+
String generatedPropertyValue = "generated";
78+
79+
scope.inTransaction(
80+
session -> {
81+
TestEntity testEntity = session.find( TestEntity.class, id );
82+
assertThat( testEntity.anEmbeddable.nonGeneratedProperty )
83+
.isEqualTo( nonGeneratedPropertyValue );
84+
assertThat( testEntity.anEmbeddable.generatedProperty )
85+
.isEqualTo( null );
86+
assertThat( testEntity.anEmbeddable.generatedButWritableProperty )
87+
.isEqualTo( null );
88+
testEntity.anEmbeddable.generatedButWritableProperty = generatedButWritablePropertyValue;
89+
testEntity.anEmbeddable.generatedProperty = generatedPropertyValue;
90+
}
91+
);
92+
93+
scope.inTransaction(
94+
session -> {
95+
TestEntity testEntity = session.find( TestEntity.class, id );
96+
assertThat( testEntity.anEmbeddable.nonGeneratedProperty )
97+
.isEqualTo( nonGeneratedPropertyValue );
98+
assertThat( testEntity.anEmbeddable.generatedProperty )
99+
.isEqualTo( null );
100+
assertThat( testEntity.anEmbeddable.generatedButWritableProperty )
101+
.isEqualTo( generatedButWritablePropertyValue );
102+
testEntity.name = "a";
103+
}
104+
);
105+
}
106+
107+
@Entity(name = "TestEntity")
108+
public static class TestEntity {
109+
@Id
110+
private Long id;
111+
112+
private String name;
113+
114+
@Embedded
115+
private AnEmbeddable anEmbeddable;
116+
117+
118+
public TestEntity() {
119+
}
120+
121+
public TestEntity(Long id, String nonGeneratedProperty) {
122+
this.id = id;
123+
this.anEmbeddable = new AnEmbeddable( nonGeneratedProperty );
124+
}
125+
126+
public TestEntity(Long id, String generatedButWritableProperty, String nonGeneratedProperty) {
127+
this.id = id;
128+
this.anEmbeddable = new AnEmbeddable( generatedButWritableProperty, nonGeneratedProperty );
129+
}
130+
131+
public TestEntity(Long id, String generatedProperty, String generatedButWritableProperty, String nonGeneratedProperty) {
132+
this.id = id;
133+
this.anEmbeddable = new AnEmbeddable( generatedProperty, generatedButWritableProperty,
134+
nonGeneratedProperty );
135+
}
136+
}
137+
138+
@Embeddable
139+
public static class AnEmbeddable {
140+
@Generated(event = EventType.UPDATE)
141+
private String generatedProperty;
142+
143+
@Generated(event = EventType.UPDATE, writable = true)
144+
private String generatedButWritableProperty;
145+
146+
private String nonGeneratedProperty;
147+
148+
149+
public AnEmbeddable() {
150+
}
151+
152+
public AnEmbeddable(String nonGeneratedProperty) {
153+
this.nonGeneratedProperty = nonGeneratedProperty;
154+
}
155+
156+
public AnEmbeddable(String generatedButWritableProperty, String nonGeneratedProperty) {
157+
this.generatedButWritableProperty = generatedButWritableProperty;
158+
this.nonGeneratedProperty = nonGeneratedProperty;
159+
}
160+
161+
public AnEmbeddable(String generatedProperty, String generatedButWritableProperty, String nonGeneratedProperty) {
162+
this.generatedProperty = generatedProperty;
163+
this.generatedButWritableProperty = generatedButWritableProperty;
164+
this.nonGeneratedProperty = nonGeneratedProperty;
165+
}
166+
}
167+
}

0 commit comments

Comments
 (0)