Skip to content

Commit e329802

Browse files
committed
[HHH-19586] Support @IdClass in Panache 2 repositories
1 parent 622f6b3 commit e329802

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;
@@ -703,9 +704,20 @@ else if ( idType != null && finalPrimaryEntity != null ) {
703704
}
704705

705706
private @Nullable TypeMirror findIdType() {
706-
Element idMember = findIdMember();
707707
TypeElement primaryEntityForTest = primaryEntity;
708-
if ( idMember != null && primaryEntityForTest != null ) {
708+
if ( primaryEntityForTest == null ) {
709+
return null;
710+
}
711+
AnnotationMirror idClass = getInheritedAnnotationMirror( this.context.getElementUtils(), primaryEntityForTest, ID_CLASS );
712+
if ( idClass != null ) {
713+
AnnotationValue value = getAnnotationValue(idClass, "value" );
714+
// I don't think this can have a null value
715+
if ( value != null ) {
716+
return (TypeMirror) value.getValue();
717+
}
718+
}
719+
Element idMember = findIdMember();
720+
if ( idMember != null ) {
709721
TypeMirror typedIdMember = this.context.getTypeUtils().asMemberOf((DeclaredType) primaryEntityForTest.asType(), idMember);
710722
return switch(typedIdMember.getKind()) {
711723
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)