Skip to content

Commit e571675

Browse files
committed
[HHH-19586] Support @IdClass in Panache 2 repositories
1 parent ddc18bb commit e571675

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror;
9696
import static org.hibernate.processor.util.TypeUtils.getAnnotationValue;
9797
import static org.hibernate.processor.util.TypeUtils.getGeneratedClassFullyQualifiedName;
98+
import static org.hibernate.processor.util.TypeUtils.getInheritedAnnotationMirror;
9899
import static org.hibernate.processor.util.TypeUtils.hasAnnotation;
99100
import static org.hibernate.processor.util.TypeUtils.implementsInterface;
100101
import static org.hibernate.processor.util.TypeUtils.primitiveClassMatchesKind;
@@ -701,9 +702,20 @@ else if ( idType != null && finalPrimaryEntity != null ) {
701702
}
702703

703704
private @Nullable TypeMirror findIdType() {
704-
Element idMember = findIdMember();
705705
TypeElement primaryEntityForTest = primaryEntity;
706-
if ( idMember != null && primaryEntityForTest != null ) {
706+
if ( primaryEntityForTest == null ) {
707+
return null;
708+
}
709+
AnnotationMirror idClass = getInheritedAnnotationMirror( this.context.getElementUtils(), primaryEntityForTest, ID_CLASS );
710+
if ( idClass != null ) {
711+
AnnotationValue value = getAnnotationValue(idClass, "value" );
712+
// I don't think this can have a null value
713+
if ( value != null ) {
714+
return (TypeMirror) value.getValue();
715+
}
716+
}
717+
Element idMember = findIdMember();
718+
if ( idMember != null ) {
707719
TypeMirror typedIdMember = this.context.getTypeUtils().asMemberOf((DeclaredType) primaryEntityForTest.asType(), idMember);
708720
return switch(typedIdMember.getKind()) {
709721
case ARRAY, DECLARED, BOOLEAN, BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE -> typedIdMember;

tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/TypeUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import javax.lang.model.type.TypeVariable;
2828
import javax.lang.model.type.WildcardType;
2929
import javax.lang.model.util.ElementFilter;
30+
import javax.lang.model.util.Elements;
3031
import javax.lang.model.util.SimpleTypeVisitor8;
3132
import javax.tools.Diagnostic;
3233
import java.util.HashMap;
@@ -247,6 +248,26 @@ public static boolean isAnnotationMirrorOfType(AnnotationMirror annotationMirror
247248
return null;
248249
}
249250

251+
/**
252+
* Checks whether the {@code Element} hosts the annotation (directly or inherited) with the given fully qualified class name.
253+
*
254+
* @param element the element to check for the hosted annotation
255+
* @param qualifiedName the fully qualified class name of the annotation to check for
256+
*
257+
* @return the annotation mirror for the specified annotation class from the {@code Element} or {@code null} in case
258+
* the {@code TypeElement} does not host the specified annotation (directly or inherited).
259+
*/
260+
public static @Nullable AnnotationMirror getInheritedAnnotationMirror(Elements elements, Element element, String qualifiedName) {
261+
assert element != null;
262+
assert qualifiedName != null;
263+
for ( AnnotationMirror mirror : elements.getAllAnnotationMirrors(element) ) {
264+
if ( isAnnotationMirrorOfType( mirror, qualifiedName ) ) {
265+
return mirror;
266+
}
267+
}
268+
return null;
269+
}
270+
250271
public static boolean hasAnnotation(Element element, String qualifiedName) {
251272
return getAnnotationMirror( element, qualifiedName ) != null;
252273
}

0 commit comments

Comments
 (0)