Skip to content

Commit a506a67

Browse files
cigalygavinking
authored andcommitted
HHH-19209 Additional test cases
- check generation of ID class for inner classes - check if record components of generated ID class are sorted by name
1 parent c654739 commit a506a67

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/mapping/hhh18829/AutoGeneratedIdClassTest.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
package org.hibernate.orm.test.mapping.hhh18829;
66

7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
79
import org.hibernate.testing.orm.junit.DomainModel;
810
import org.hibernate.testing.orm.junit.JiraKey;
911
import org.hibernate.testing.orm.junit.SessionFactory;
@@ -15,7 +17,7 @@
1517

1618
import static org.junit.jupiter.api.Assertions.assertEquals;
1719

18-
@DomainModel(annotatedClasses = EmployeeWithoutIdClass.class)
20+
@DomainModel(annotatedClasses = {EmployeeWithoutIdClass.class, AutoGeneratedIdClassTest.Inner.class})
1921
@JiraKey(" HHH-18829")
2022
@SessionFactory
2123
public class AutoGeneratedIdClassTest {
@@ -35,18 +37,51 @@ void setUp(SessionFactoryScope sessionFactoryScope) {
3537
two.address = "1600 Pennsylvania Avenue";
3638
sess.persist( two );
3739
} );
40+
sessionFactoryScope.inTransaction( sess -> {
41+
final var one = new Inner();
42+
one.empName = "John Doe";
43+
one.empId = 1;
44+
one.address = "10 Downing Street, SW1A 2AA";
45+
sess.persist( one );
46+
47+
final var two = new Inner();
48+
two.empName = "Dave Default";
49+
two.empId = 13;
50+
two.address = "1600 Pennsylvania Avenue";
51+
sess.persist( two );
52+
} );
3853
}
3954

4055
@Test
4156
public void test(SessionFactoryScope sessionFactoryScope)
4257
throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
4358
final var idClass = Class.forName( EmployeeWithoutIdClass.class.getName() + "_$Id" );
44-
final var id = idClass.getConstructors()[0].newInstance( "John Doe", 1 );
59+
final var id = idClass.getConstructors()[0].newInstance( 1, "John Doe" );
4560
final var employees = sessionFactoryScope.fromSession(
46-
sess -> sess.createQuery( "from EmployeeWithoutIdClass where id=:id", EmployeeWithoutIdClass.class ).setParameter( "id", id )
61+
sess -> sess.createQuery( "from EmployeeWithoutIdClass where id=:id", EmployeeWithoutIdClass.class )
62+
.setParameter( "id", id )
4763
.getResultList()
4864
);
4965
assertEquals( 1, employees.size() );
5066
assertEquals( "10 Downing Street, SW1A 2AA", employees.get( 0 ).address );
5167
}
68+
69+
@Test
70+
public void innerEntityClassTest(SessionFactoryScope sessionFactoryScope)
71+
throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
72+
final var idClass = Class.forName( AutoGeneratedIdClassTest .class.getName() + "_$Inner_$Id" );
73+
final var id = idClass.getConstructors()[0].newInstance( 13, "Dave Default" );
74+
final Inner employee = sessionFactoryScope.fromSession(
75+
sess -> sess.find( Inner.class, id ) );
76+
assertEquals( "1600 Pennsylvania Avenue", employee.address );
77+
}
78+
79+
@Entity
80+
static class Inner {
81+
@Id
82+
String empName;
83+
@Id
84+
Integer empId;
85+
String address;
86+
}
5287
}

tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/hhh18829/AutoGeneratedIdClassTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void test() {
2929
System.out.println( TestUtil.getMetaModelSourceAsString( Address.class ) );
3030
System.out.println( TestUtil.getMetaModelSourceAsString( EmployeeWithIdClass.class ) );
3131

32-
checkIfIdClassIsGenerated( Employee.class, new String[] {"empName", "empId"} );
32+
checkIfIdClassIsGenerated( Employee.class, new String[] {"empId", "empName"} );
3333
checkIfIdClassIsGenerated( AnotherEmployee.class, new String[] {"empId", "empName"} );
3434

3535
final var clazz = getMetamodelClassFor( EmployeeWithIdClass.class );
@@ -38,6 +38,18 @@ public void test() {
3838
"EmployeeWithIdClass_ should not have inner class Id" );
3939
}
4040

41+
@Test
42+
@WithClasses(value = {Outer.Inner.class, Outer.Super.class})
43+
@TestForIssue(jiraKey = "HHH-18829")
44+
public void testInner() {
45+
System.out.println( TestUtil.getMetaModelSourceAsString( Outer.class ) );
46+
System.out.println( TestUtil.getMetaModelSourceAsString( Outer.Inner.class ) );
47+
System.out.println( TestUtil.getMetaModelSourceAsString( Outer.Super.class ) );
48+
49+
checkIfIdClassIsGenerated( Outer.Inner.class, new String[] {"empId", "empName"} );
50+
checkIfIdClassIsGenerated( Outer.Super.class, new String[] {"empId", "empName"} );
51+
}
52+
4153
private static void checkIfIdClassIsGenerated(Class<?> entityClass, String[] idComponentNames) {
4254
final var clazz = getMetamodelClassFor( entityClass );
4355
final var maybeIdClass = Arrays.stream( clazz.getClasses() )
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.test.hhh18829;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
import jakarta.persistence.MappedSuperclass;
10+
11+
public class Outer {
12+
13+
@Entity
14+
static class Inner {
15+
@Id
16+
String empName;
17+
@Id
18+
Integer empId;
19+
String address;
20+
}
21+
22+
@MappedSuperclass
23+
static class Super {
24+
@Id
25+
String empName;
26+
@Id
27+
Integer empId;
28+
String address;
29+
}
30+
}

0 commit comments

Comments
 (0)