|
143 | 143 | import static org.hibernate.boot.model.internal.HCANNHelper.findContainingAnnotations;
|
144 | 144 | import static org.hibernate.boot.model.internal.InheritanceState.getInheritanceStateOfSuperEntity;
|
145 | 145 | import static org.hibernate.boot.model.internal.PropertyBinder.addElementsOfClass;
|
| 146 | +import static org.hibernate.boot.model.internal.PropertyBinder.hasIdAnnotation; |
146 | 147 | import static org.hibernate.boot.model.internal.PropertyBinder.processElementAnnotations;
|
147 | 148 | import static org.hibernate.boot.model.internal.PropertyHolderBuilder.buildPropertyHolder;
|
148 | 149 | import static org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle.fromResultCheckStyle;
|
@@ -960,46 +961,61 @@ private void processIdPropertiesIfNotAlready(
|
960 | 961 | Set<String> idPropertiesIfIdClass,
|
961 | 962 | ElementsToProcess elementsToProcess,
|
962 | 963 | Map<XClass, InheritanceState> inheritanceStates) {
|
963 |
| - |
964 | 964 | final Set<String> missingIdProperties = new HashSet<>( idPropertiesIfIdClass );
|
| 965 | + final Set<String> missingEntityProperties = new HashSet<>(); |
965 | 966 | for ( PropertyData propertyAnnotatedElement : elementsToProcess.getElements() ) {
|
966 | 967 | final String propertyName = propertyAnnotatedElement.getPropertyName();
|
967 | 968 | if ( !idPropertiesIfIdClass.contains( propertyName ) ) {
|
968 |
| - boolean subclassAndSingleTableStrategy = |
969 |
| - inheritanceState.getType() == InheritanceType.SINGLE_TABLE |
970 |
| - && inheritanceState.hasParents(); |
971 |
| - processElementAnnotations( |
972 |
| - propertyHolder, |
973 |
| - subclassAndSingleTableStrategy |
974 |
| - ? Nullability.FORCED_NULL |
975 |
| - : Nullability.NO_CONSTRAINT, |
976 |
| - propertyAnnotatedElement, |
977 |
| - generators, |
978 |
| - this, |
979 |
| - false, |
980 |
| - false, |
981 |
| - false, |
982 |
| - context, |
983 |
| - inheritanceStates |
984 |
| - ); |
| 969 | + if ( !idPropertiesIfIdClass.isEmpty() && !isIgnoreIdAnnotations() |
| 970 | + && hasIdAnnotation( propertyAnnotatedElement.getProperty() ) ) { |
| 971 | + missingEntityProperties.add( propertyName ); |
| 972 | + } |
| 973 | + else { |
| 974 | + boolean subclassAndSingleTableStrategy = |
| 975 | + inheritanceState.getType() == InheritanceType.SINGLE_TABLE |
| 976 | + && inheritanceState.hasParents(); |
| 977 | + processElementAnnotations( |
| 978 | + propertyHolder, |
| 979 | + subclassAndSingleTableStrategy |
| 980 | + ? Nullability.FORCED_NULL |
| 981 | + : Nullability.NO_CONSTRAINT, |
| 982 | + propertyAnnotatedElement, |
| 983 | + generators, |
| 984 | + this, |
| 985 | + false, |
| 986 | + false, |
| 987 | + false, |
| 988 | + context, |
| 989 | + inheritanceStates |
| 990 | + ); |
| 991 | + } |
985 | 992 | }
|
986 | 993 | else {
|
987 | 994 | missingIdProperties.remove( propertyName );
|
988 | 995 | }
|
989 | 996 | }
|
990 | 997 |
|
991 |
| - if ( missingIdProperties.size() != 0 ) { |
992 |
| - final StringBuilder missings = new StringBuilder(); |
993 |
| - for ( String property : missingIdProperties ) { |
994 |
| - if ( missings.length() > 0 ) { |
995 |
| - missings.append(", "); |
996 |
| - } |
997 |
| - missings.append("'").append( property ).append( "'" ); |
998 |
| - } |
| 998 | + if ( !missingIdProperties.isEmpty() ) { |
999 | 999 | throw new AnnotationException( "Entity '" + persistentClass.getEntityName()
|
1000 |
| - + "' has an '@IdClass' with properties " + missings |
| 1000 | + + "' has an '@IdClass' with properties " + getMissingPropertiesString( missingIdProperties ) |
1001 | 1001 | + " which do not match properties of the entity class" );
|
1002 | 1002 | }
|
| 1003 | + else if ( !missingEntityProperties.isEmpty() ) { |
| 1004 | + throw new AnnotationException( "Entity '" + persistentClass.getEntityName() |
| 1005 | + + "' has '@Id' annotated properties " + getMissingPropertiesString( missingEntityProperties ) |
| 1006 | + + " which do not match properties of the specified '@IdClass'" ); |
| 1007 | + } |
| 1008 | + } |
| 1009 | + |
| 1010 | + private static String getMissingPropertiesString(Set<String> propertyNames) { |
| 1011 | + final StringBuilder sb = new StringBuilder(); |
| 1012 | + for ( String property : propertyNames ) { |
| 1013 | + if ( sb.length() > 0 ) { |
| 1014 | + sb.append( ", " ); |
| 1015 | + } |
| 1016 | + sb.append( "'" ).append( property ).append( "'" ); |
| 1017 | + } |
| 1018 | + return sb.toString(); |
1003 | 1019 | }
|
1004 | 1020 |
|
1005 | 1021 | private static PersistentClass makePersistentClass(
|
|
0 commit comments