HHH-18179 incorrect warning issued for mappedBy with property access

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-05-29 17:16:28 +02:00
parent 08d0d78147
commit 9ac37a19c0
1 changed files with 16 additions and 2 deletions

View File

@ -1005,7 +1005,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
final AnnotationValue annotationVal = final AnnotationValue annotationVal =
castNonNull(getAnnotationValue(annotation, "mappedBy")); castNonNull(getAnnotationValue(annotation, "mappedBy"));
for ( Element member : context.getAllMembers(assocTypeElement) ) { for ( Element member : context.getAllMembers(assocTypeElement) ) {
if ( propertyName(this, member).contentEquals(mappedBy) ) { if ( propertyName(this, member).contentEquals(mappedBy)
&& compatibleAccess(assocTypeElement, member) ) {
validateBackRef(memberOfClass, annotation, assocTypeElement, member, annotationVal); validateBackRef(memberOfClass, annotation, assocTypeElement, member, annotationVal);
return; return;
} }
@ -1018,6 +1019,19 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
} }
private boolean compatibleAccess(TypeElement assocTypeElement, Element member) {
final AccessType memberAccessType = determineAnnotationSpecifiedAccessType( member );
final AccessType accessType = memberAccessType == null ? getAccessType(assocTypeElement) : memberAccessType;
switch ( member.getKind() ) {
case FIELD:
return accessType == AccessType.FIELD;
case METHOD:
return accessType == AccessType.PROPERTY;
default:
return false;
}
}
private void validateBackRef( private void validateBackRef(
Element memberOfClass, Element memberOfClass,
AnnotationMirror annotation, AnnotationMirror annotation,
@ -2106,7 +2120,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private AccessType getAccessType(TypeElement entity) { private AccessType getAccessType(TypeElement entity) {
final String entityClassName = entity.getQualifiedName().toString(); final String entityClassName = entity.getQualifiedName().toString();
determineAccessTypeForHierarchy(entity, context ); determineAccessTypeForHierarchy( entity, context );
return castNonNull( context.getAccessTypeInfo( entityClassName ) ).getAccessType(); return castNonNull( context.getAccessTypeInfo( entityClassName ) ).getAccessType();
} }