11
11
import jakarta .persistence .Id ;
12
12
import jakarta .persistence .SecondaryTable ;
13
13
import jakarta .persistence .Table ;
14
+ import org .hibernate .AnnotationException ;
15
+ import org .hibernate .boot .MetadataSources ;
16
+ import org .hibernate .boot .registry .StandardServiceRegistry ;
14
17
import org .hibernate .testing .orm .junit .DomainModel ;
15
18
import org .hibernate .testing .orm .junit .JiraKey ;
19
+ import org .hibernate .testing .orm .junit .ServiceRegistryScope ;
16
20
import org .hibernate .testing .orm .junit .SessionFactory ;
17
21
import org .hibernate .testing .orm .junit .SessionFactoryScope ;
18
22
import org .junit .jupiter .api .BeforeAll ;
19
23
import org .junit .jupiter .api .Test ;
20
24
21
25
import static org .assertj .core .api .Assertions .assertThat ;
26
+ import static org .assertj .core .api .Assertions .fail ;
22
27
23
28
@ JiraKey ("HHH-19542" )
24
29
@ DomainModel (annotatedClasses = {
25
30
RecordNestedEmbeddedWithASecondaryTableTest .UserEntity .class
26
31
})
27
32
@ SessionFactory
28
- public class RecordNestedEmbeddedWithASecondaryTableTest {
33
+ class RecordNestedEmbeddedWithASecondaryTableTest {
29
34
30
35
private UserEntity user ;
31
36
32
37
@ BeforeAll
33
- public void prepare (SessionFactoryScope scope ) {
38
+ void prepare (SessionFactoryScope scope ) {
34
39
scope .inTransaction ( session -> {
35
40
Person person = new Person ( new FullName ( "Sylvain" , "Lecoy" ), 38 );
36
41
user = new UserEntity ( person );
@@ -39,7 +44,7 @@ public void prepare(SessionFactoryScope scope) {
39
44
}
40
45
41
46
@ Test
42
- public void test (SessionFactoryScope scope ) {
47
+ void test (SessionFactoryScope scope ) {
43
48
scope .inTransaction (session -> {
44
49
UserEntity entity = session .find ( UserEntity .class , user .id );
45
50
assertThat ( entity ).isNotNull ();
@@ -51,10 +56,23 @@ public void test(SessionFactoryScope scope) {
51
56
});
52
57
}
53
58
59
+ @ Test
60
+ void test (ServiceRegistryScope scope ) {
61
+ final StandardServiceRegistry registry = scope .getRegistry ();
62
+ final MetadataSources sources = new MetadataSources ( registry ).addAnnotatedClass ( UserEntity1 .class );
63
+
64
+ try {
65
+ sources .buildMetadata ();
66
+ fail ( "Expecting to fail" );
67
+ } catch (AnnotationException expected ) {
68
+ assertThat ( expected ).hasMessageContaining ( "all properties of the embeddable class must map to the same table" );
69
+ }
70
+ }
71
+
54
72
@ Entity
55
73
@ Table (name = "UserEntity" )
56
74
@ SecondaryTable (name = "Person" )
57
- public static class UserEntity {
75
+ static class UserEntity {
58
76
@ Id
59
77
@ GeneratedValue
60
78
private Integer id ;
@@ -71,19 +89,54 @@ protected UserEntity() {
71
89
}
72
90
73
91
@ Embeddable
74
- public record Person (
92
+ record Person (
75
93
FullName fullName ,
76
94
@ Column (table = "Person" )
77
95
Integer age ) {
78
96
79
97
}
80
98
81
99
@ Embeddable
82
- public record FullName (
100
+ record FullName (
83
101
@ Column (table = "Person" )
84
102
String firstName ,
85
103
@ Column (table = "Person" )
86
104
String lastName ) {
87
105
88
106
}
107
+
108
+ @ Entity
109
+ @ Table (name = "UserEntity" )
110
+ @ SecondaryTable (name = "Person" )
111
+ public static class UserEntity1 {
112
+ @ Id
113
+ @ GeneratedValue
114
+ private Integer id ;
115
+ private Person1 person ;
116
+
117
+ public UserEntity1 (
118
+ final Person1 person ) {
119
+ this .person = person ;
120
+ }
121
+
122
+ protected UserEntity1 () {
123
+
124
+ }
125
+ }
126
+
127
+ @ Embeddable
128
+ public record Person1 (
129
+ FullName1 fullName ,
130
+ @ Column (table = "Person" )
131
+ Integer age ) {
132
+
133
+ }
134
+
135
+ @ Embeddable
136
+ public record FullName1 (
137
+ @ Column (table = "Person" )
138
+ String firstName ,
139
+ String lastName ) {
140
+
141
+ }
89
142
}
0 commit comments