Skip to content

Commit ddcdf1e

Browse files
cigalygavinking
authored andcommitted
HHH-19209 Fixed name resolution for autogenerated ID class name when entity class is inner class
1 parent a506a67 commit ddcdf1e

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ private ClassDetails idClassDetails(InheritanceState inheritanceState, ClassDeta
525525
if ( idClassAnn == null ) {
526526
try {
527527
// look for an Id class generated by Hibernate Processor as an inner class of static metamodel
528-
final String generatedIdClassName = inheritanceState.getClassDetails().getClassName() + "_$Id";
528+
final Class<Object> javaClass = inheritanceState.getClassDetails().toJavaClass();
529+
final String generatedIdClassName = getGeneratedClassName( javaClass ) + "$Id";
529530
return classDetailsRegistry.resolveClassDetails( generatedIdClassName );
530531
}
531532
catch (RuntimeException e) {
@@ -537,6 +538,12 @@ private ClassDetails idClassDetails(InheritanceState inheritanceState, ClassDeta
537538
}
538539
}
539540

541+
private static String getGeneratedClassName(Class<?> javaClass) {
542+
return javaClass.isMemberClass()
543+
? getGeneratedClassName( javaClass.getEnclosingClass() ) + "$" + javaClass.getSimpleName() + "_"
544+
: javaClass.getName() + "_";
545+
}
546+
540547
private Component createMapperProperty(
541548
Map<ClassDetails, InheritanceState> inheritanceStates,
542549
PersistentClass persistentClass,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void setUp(SessionFactoryScope sessionFactoryScope) {
5656
public void test(SessionFactoryScope sessionFactoryScope)
5757
throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
5858
final var idClass = Class.forName( EmployeeWithoutIdClass.class.getName() + "_$Id" );
59-
final var id = idClass.getConstructors()[0].newInstance( 1, "John Doe" );
59+
final var id = idClass.getConstructors()[0].newInstance( "John Doe", 1 );
6060
final var employees = sessionFactoryScope.fromSession(
6161
sess -> sess.createQuery( "from EmployeeWithoutIdClass where id=:id", EmployeeWithoutIdClass.class )
6262
.setParameter( "id", id )
@@ -70,7 +70,7 @@ public void test(SessionFactoryScope sessionFactoryScope)
7070
public void innerEntityClassTest(SessionFactoryScope sessionFactoryScope)
7171
throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
7272
final var idClass = Class.forName( AutoGeneratedIdClassTest .class.getName() + "_$Inner_$Id" );
73-
final var id = idClass.getConstructors()[0].newInstance( 13, "Dave Default" );
73+
final var id = idClass.getConstructors()[0].newInstance( "Dave Default", 13 );
7474
final Inner employee = sessionFactoryScope.fromSession(
7575
sess -> sess.find( Inner.class, id ) );
7676
assertEquals( "1600 Pennsylvania Avenue", employee.address );

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

Lines changed: 3 additions & 3 deletions
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[] {"empId", "empName"} );
32+
checkIfIdClassIsGenerated( Employee.class, new String[] {"empName", "empId"} );
3333
checkIfIdClassIsGenerated( AnotherEmployee.class, new String[] {"empId", "empName"} );
3434

3535
final var clazz = getMetamodelClassFor( EmployeeWithIdClass.class );
@@ -46,8 +46,8 @@ public void testInner() {
4646
System.out.println( TestUtil.getMetaModelSourceAsString( Outer.Inner.class ) );
4747
System.out.println( TestUtil.getMetaModelSourceAsString( Outer.Super.class ) );
4848

49-
checkIfIdClassIsGenerated( Outer.Inner.class, new String[] {"empId", "empName"} );
50-
checkIfIdClassIsGenerated( Outer.Super.class, new String[] {"empId", "empName"} );
49+
checkIfIdClassIsGenerated( Outer.Inner.class, new String[] {"empName", "empId"} );
50+
checkIfIdClassIsGenerated( Outer.Super.class, new String[] {"empName", "empId"} );
5151
}
5252

5353
private static void checkIfIdClassIsGenerated(Class<?> entityClass, String[] idComponentNames) {

0 commit comments

Comments
 (0)