Skip to content

Commit 6d6e1b7

Browse files
committed
HHH-18829 Hibernate Processor - ID class generation where needed
1 parent b033e2d commit 6d6e1b7

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.hibernate.processor.model.MetaAttribute;
88
import org.hibernate.processor.model.Metamodel;
9+
import org.hibernate.processor.util.Constants;
910

1011
import javax.annotation.processing.FilerException;
1112
import javax.lang.model.element.ElementKind;
@@ -20,6 +21,9 @@
2021
import java.time.format.DateTimeFormatter;
2122
import java.util.List;
2223

24+
import static org.hibernate.processor.util.TypeUtils.hasAnnotation;
25+
import static org.hibernate.processor.util.TypeUtils.isAnnotationMirrorOfType;
26+
2327
/**
2428
* Helper class to write the actual metamodel class using the {@link javax.annotation.processing.Filer} API.
2529
*
@@ -100,6 +104,8 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
100104

101105
pw.println();
102106

107+
generateIdClassIfNeeded( entity, pw );
108+
103109
final List<MetaAttribute> members = entity.getMembers();
104110
for ( MetaAttribute metaMember : members ) {
105111
if ( metaMember.hasStringAttribute() ) {
@@ -229,4 +235,24 @@ private static String writeStaticMetaModelAnnotation(Metamodel entity) {
229235
: "jakarta.persistence.metamodel.StaticMetamodel";
230236
return "@" + entity.importType( annotation ) + "(" + entity.getSimpleName() + ".class)";
231237
}
238+
239+
private static void generateIdClassIfNeeded(Metamodel entity, PrintWriter pw) {
240+
if ( hasAnnotation( entity.getElement(), Constants.ID_CLASS ) ) {
241+
return;
242+
}
243+
244+
final List<String> idFields = entity.getMembers().stream()
245+
.filter( a -> a.inheritedAnnotations().stream()
246+
.anyMatch( m -> isAnnotationMirrorOfType( m, Constants.ID ) )
247+
)
248+
.map( a -> entity.importType( a.getTypeDeclaration() ) + " " + a.getPropertyName() )
249+
.toList();
250+
251+
if ( idFields.size() > 1 ) {
252+
pw.print( "public record Id(" );
253+
pw.print( String.join( ", ", idFields ) );
254+
pw.println( ") {}" );
255+
pw.println();
256+
}
257+
}
232258
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
*/
55
package org.hibernate.processor.annotation;
66

7+
import javax.lang.model.element.AnnotationMirror;
78
import javax.lang.model.element.Element;
89

910
import org.hibernate.processor.model.MetaSingleAttribute;
1011
import org.hibernate.processor.util.Constants;
1112

13+
import java.util.ArrayList;
14+
import java.util.List;
15+
1216
/**
1317
* @author Max Andersen
1418
* @author Hardy Ferentschik
@@ -24,4 +28,9 @@ public AnnotationMetaSingleAttribute(AnnotationMetaEntity parent, Element elemen
2428
public final String getMetaType() {
2529
return Constants.SINGULAR_ATTRIBUTE;
2630
}
31+
32+
@Override
33+
public List<AnnotationMirror> inheritedAnnotations() {
34+
return new ArrayList<>(element.getAnnotationMirrors());
35+
}
2736
}

0 commit comments

Comments
 (0)