1111import  jakarta .persistence .Id ;
1212import  jakarta .persistence .SecondaryTable ;
1313import  jakarta .persistence .Table ;
14+ import  org .hibernate .AnnotationException ;
15+ import  org .hibernate .boot .MetadataSources ;
16+ import  org .hibernate .boot .registry .StandardServiceRegistry ;
1417import  org .hibernate .testing .orm .junit .DomainModel ;
1518import  org .hibernate .testing .orm .junit .JiraKey ;
19+ import  org .hibernate .testing .orm .junit .ServiceRegistryScope ;
1620import  org .hibernate .testing .orm .junit .SessionFactory ;
1721import  org .hibernate .testing .orm .junit .SessionFactoryScope ;
1822import  org .junit .jupiter .api .BeforeAll ;
1923import  org .junit .jupiter .api .Test ;
2024
2125import  static  org .assertj .core .api .Assertions .assertThat ;
26+ import  static  org .assertj .core .api .Assertions .fail ;
2227
2328@ JiraKey ("HHH-19542" )
2429@ DomainModel (annotatedClasses  = {
2530		RecordNestedEmbeddedWithASecondaryTableTest .UserEntity .class 
2631})
2732@ SessionFactory 
28- public   class  RecordNestedEmbeddedWithASecondaryTableTest  {
33+ class  RecordNestedEmbeddedWithASecondaryTableTest  {
2934
3035	private  UserEntity  user ;
3136
3237	@ BeforeAll 
33- 	public   void  prepare (SessionFactoryScope  scope ) {
38+ 	void  prepare (SessionFactoryScope  scope ) {
3439		scope .inTransaction ( session  -> {
3540			Person  person  = new  Person ( new  FullName ( "Sylvain" , "Lecoy"  ), 38  );
3641			user  = new  UserEntity ( person  );
@@ -39,7 +44,7 @@ public void prepare(SessionFactoryScope scope) {
3944	}
4045
4146	@ Test 
42- 	public   void  test (SessionFactoryScope  scope ) {
47+ 	void  test (SessionFactoryScope  scope ) {
4348		scope .inTransaction (session  -> {
4449			UserEntity  entity  = session .find ( UserEntity .class , user .id  );
4550			assertThat ( entity  ).isNotNull ();
@@ -51,10 +56,23 @@ public void test(SessionFactoryScope scope) {
5156		});
5257	}
5358
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+ 
5472	@ Entity 
5573	@ Table (name  = "UserEntity" )
5674	@ SecondaryTable (name  = "Person" )
57- 	public   static  class  UserEntity  {
75+ 	static  class  UserEntity  {
5876		@ Id 
5977		@ GeneratedValue 
6078		private  Integer  id ;
@@ -71,19 +89,54 @@ protected UserEntity() {
7189	}
7290
7391	@ Embeddable 
74- 	public   record  Person (
92+ 	record  Person (
7593			FullName  fullName ,
7694			@ Column (table  = "Person" )
7795			Integer  age ) {
7896
7997	}
8098
8199	@ Embeddable 
82- 	public   record  FullName (
100+ 	record  FullName (
83101			@ Column (table  = "Person" )
84102			String  firstName ,
85103			@ Column (table  = "Person" )
86104			String  lastName ) {
87105
88106	}
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+ 	}
89142}
0 commit comments