@@ -835,8 +835,9 @@ private static void processIdClassElements(
835835
836836 for ( int i = 0 ; i < classElements .size (); i ++ ) {
837837 final PropertyData idClassPropertyData = classElements .get ( i );
838+ final String propertyName = idClassPropertyData .getPropertyName ();
838839 final PropertyData entityPropertyData =
839- baseClassElementsByName .get ( idClassPropertyData . getPropertyName () );
840+ baseClassElementsByName .get ( propertyName );
840841 if ( propertyHolder .isInIdClass () ) {
841842 if ( entityPropertyData == null ) {
842843 throw new AnnotationException (
@@ -852,11 +853,38 @@ private static void processIdClassElements(
852853 //the annotation overriding will be dealt with by a mechanism similar to @MapsId
853854 continue ;
854855 }
856+ if ( !hasCompatibleType ( idClassPropertyData .getTypeName (), entityPropertyData .getTypeName () ) ) {
857+ throw new AnnotationException (
858+ "Property '" + propertyName + "' in @IdClass '" + idClassPropertyData .getDeclaringClass ().getName ()
859+ + "' doesn't match type in entity class '" + baseInferredData .getPropertyType ().getName ()
860+ + "' (expected '" + entityPropertyData .getTypeName () + "' but was '" + idClassPropertyData .getTypeName () + "')"
861+ );
862+ }
855863 }
856864 classElements .set ( i , entityPropertyData ); //this works since they are in the same order
857865 }
858866 }
859867
868+ private static boolean hasCompatibleType ( String typeNameInIdClass , String typeNameInEntityClass ) {
869+ if ( typeNameInIdClass .equals ( typeNameInEntityClass ) ) {
870+ return true ;
871+ }
872+ return toWrapper ( typeNameInIdClass ).equals ( typeNameInEntityClass ) || typeNameInIdClass .equals (
873+ toWrapper ( typeNameInEntityClass ) );
874+ }
875+
876+ private static String toWrapper (String value ) {
877+ if ( value .indexOf ( '.' ) < 0 ) {
878+ String wrapper = switch ( value ) {
879+ case "int" -> "Integer" ;
880+ case "char" -> "Character" ;
881+ default -> Character .toUpperCase ( value .charAt ( 0 ) ) + value .substring ( 1 );
882+ };
883+ return "java.lang." + wrapper ;
884+ }
885+ return value ;
886+ }
887+
860888 static Component createEmbeddable (
861889 PropertyHolder propertyHolder ,
862890 PropertyData inferredData ,
0 commit comments