|
6 | 6 |
|
7 | 7 | import org.hibernate.processor.model.MetaAttribute; |
8 | 8 | import org.hibernate.processor.model.Metamodel; |
| 9 | +import org.hibernate.processor.util.Constants; |
9 | 10 |
|
10 | 11 | import javax.annotation.processing.FilerException; |
11 | 12 | import javax.lang.model.element.ElementKind; |
|
20 | 21 | import java.time.format.DateTimeFormatter; |
21 | 22 | import java.util.List; |
22 | 23 |
|
| 24 | +import static org.hibernate.processor.util.TypeUtils.hasAnnotation; |
| 25 | +import static org.hibernate.processor.util.TypeUtils.isAnnotationMirrorOfType; |
| 26 | + |
23 | 27 | /** |
24 | 28 | * Helper class to write the actual metamodel class using the {@link javax.annotation.processing.Filer} API. |
25 | 29 | * |
@@ -100,6 +104,8 @@ private static StringBuffer generateBody(Metamodel entity, Context context) { |
100 | 104 |
|
101 | 105 | pw.println(); |
102 | 106 |
|
| 107 | + generateIdClassIfNeeded( entity, pw ); |
| 108 | + |
103 | 109 | final List<MetaAttribute> members = entity.getMembers(); |
104 | 110 | for ( MetaAttribute metaMember : members ) { |
105 | 111 | if ( metaMember.hasStringAttribute() ) { |
@@ -229,4 +235,24 @@ private static String writeStaticMetaModelAnnotation(Metamodel entity) { |
229 | 235 | : "jakarta.persistence.metamodel.StaticMetamodel"; |
230 | 236 | return "@" + entity.importType( annotation ) + "(" + entity.getSimpleName() + ".class)"; |
231 | 237 | } |
| 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 | + } |
232 | 258 | } |
0 commit comments