diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/Context.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/Context.java index 4bece049ce..97489b663f 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/Context.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/Context.java @@ -87,6 +87,7 @@ public final class Context { private boolean addTransactionScopedAnnotation; private AccessType persistenceUnitDefaultAccessType; private boolean generateJakartaDataStaticMetamodel; + private boolean quarkusInjection; // keep track of all classes for which model have been generated private final Set generatedModelClasses = new HashSet<>(); @@ -196,6 +197,14 @@ public final class Context { this.addTransactionScopedAnnotation = addTransactionScopedAnnotation; } + public boolean isQuarkusInjection() { + return quarkusInjection; + } + + public void setQuarkusInjection(boolean quarkusInjection) { + this.quarkusInjection = quarkusInjection; + } + public Elements getElementUtils() { return processingEnvironment.getElementUtils(); } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java index b08ea6888f..fec6090c0d 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/JPAMetaModelEntityProcessor.java @@ -170,12 +170,16 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor { final PackageElement jakartaDataPackage = context.getProcessingEnvironment().getElementUtils() .getPackageElement( "jakarta.data" ); + final PackageElement quarkusOrmPackage = + context.getProcessingEnvironment().getElementUtils() + .getPackageElement( "io.quarkus.hibernate.orm" ); context.setAddInjectAnnotation( jakartaInjectPackage != null ); context.setAddNonnullAnnotation( jakartaAnnotationPackage != null ); context.setAddGeneratedAnnotation( jakartaAnnotationPackage != null ); context.setAddDependentAnnotation( jakartaContextPackage != null ); context.setAddTransactionScopedAnnotation( jakartaTransactionsPackage != null ); + context.setQuarkusInjection( quarkusOrmPackage != null ); final Map options = environment.getOptions(); @@ -250,7 +254,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor { final TypeElement typeElement = context.getElementUtils().getTypeElement( elementName ); try { final AnnotationMetaEntity metaEntity = - AnnotationMetaEntity.create( typeElement, context, false, false ); + AnnotationMetaEntity.create( typeElement, context ); context.addMetaAuxiliary( metaEntity.getQualifiedName(), metaEntity ); context.removeElementToRedo( elementName ); } @@ -278,7 +282,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor { || provider.equalsIgnoreCase("hibernate") ) { context.logMessage( Diagnostic.Kind.OTHER, "Processing repository class '" + element + "'" ); final AnnotationMetaEntity metaEntity = - AnnotationMetaEntity.create( typeElement, context, false, false ); + AnnotationMetaEntity.create( typeElement, context ); context.addMetaAuxiliary( metaEntity.getQualifiedName(), metaEntity ); } } @@ -287,7 +291,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor { if ( hasAnnotation( member, HQL, SQL, FIND ) ) { context.logMessage( Diagnostic.Kind.OTHER, "Processing annotated class '" + element + "'" ); final AnnotationMetaEntity metaEntity = - AnnotationMetaEntity.create( typeElement, context, false, false ); + AnnotationMetaEntity.create( typeElement, context ); context.addMetaAuxiliary( metaEntity.getQualifiedName(), metaEntity ); break; } @@ -454,7 +458,8 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor { = hasAnnotation( element, EMBEDDABLE, MAPPED_SUPERCLASS ); final AnnotationMetaEntity metaEntity = AnnotationMetaEntity.create( typeElement, context, - requiresLazyMemberInitialization, true ); + requiresLazyMemberInitialization, + true, false ); if ( alreadyExistingMetaEntity != null ) { metaEntity.mergeInMembers( alreadyExistingMetaEntity ); } @@ -465,7 +470,8 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor { && alreadyExistingMetaEntity == null ) { final AnnotationMetaEntity dataMetaEntity = AnnotationMetaEntity.create( typeElement, context, - requiresLazyMemberInitialization, true, true ); + requiresLazyMemberInitialization, + true, true ); // final Metamodel alreadyExistingDataMetaEntity = // tryGettingExistingDataEntityFromContext( mirror, '_' + qualifiedName ); // if ( alreadyExistingDataMetaEntity != null ) { @@ -481,7 +487,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor { private void handleRootElementAuxiliaryAnnotationMirrors(final Element element) { if ( element instanceof TypeElement ) { final AnnotationMetaEntity metaEntity = - AnnotationMetaEntity.create( (TypeElement) element, context, false, false ); + AnnotationMetaEntity.create( (TypeElement) element, context ); context.addMetaAuxiliary( metaEntity.getQualifiedName(), metaEntity ); } else if ( element instanceof PackageElement ) { diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java index 678bc96d5b..77d3f0ea76 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java @@ -101,6 +101,7 @@ public class AnnotationMetaEntity extends AnnotationMeta { private final Context context; private final boolean managed; private boolean jakartaDataRepository; + private final boolean quarkusInjection; private String qualifiedName; private final boolean jakartaDataStaticModel; @@ -138,25 +139,28 @@ public class AnnotationMetaEntity extends AnnotationMeta { private final Map memberTypes = new HashMap<>(); - public AnnotationMetaEntity(TypeElement element, Context context, boolean managed, boolean jakartaData) { + public AnnotationMetaEntity( + TypeElement element, Context context, boolean managed, + boolean jakartaDataStaticMetamodel) { this.element = element; this.context = context; this.managed = managed; this.members = new HashMap<>(); + this.quarkusInjection = context.isQuarkusInjection(); this.importContext = new ImportContextImpl( getPackageName( context, element ) ); - jakartaDataStaticModel = jakartaData; + jakartaDataStaticModel = jakartaDataStaticMetamodel; + } + + public static AnnotationMetaEntity create(TypeElement element, Context context) { + return create( element,context, false, false, false ); } public static AnnotationMetaEntity create( TypeElement element, Context context, - boolean lazilyInitialised, boolean managed) { - return create( element,context, lazilyInitialised, managed, false ); - } - - public static AnnotationMetaEntity create( - TypeElement element, Context context, - boolean lazilyInitialised, boolean managed, boolean jakartaData) { - final AnnotationMetaEntity annotationMetaEntity = new AnnotationMetaEntity( element, context, managed, jakartaData ); + boolean lazilyInitialised, boolean managed, + boolean jakartaData) { + final AnnotationMetaEntity annotationMetaEntity = + new AnnotationMetaEntity( element, context, managed, jakartaData ); if ( !lazilyInitialised ) { annotationMetaEntity.init(); } @@ -293,7 +297,7 @@ public class AnnotationMetaEntity extends AnnotationMeta { @Override public String scope() { - if (jakartaDataRepository) { + if (jakartaDataRepository && !quarkusInjection) { return context.addTransactionScopedAnnotation() ? "javax.transaction.TransactionScoped" : "jakarta.enterprise.context.RequestScoped"; @@ -345,7 +349,7 @@ public class AnnotationMetaEntity extends AnnotationMeta { sessionType = HIB_STATELESS_SESSION; addDaoConstructor( null ); } - if ( jakartaDataRepository ) { + if ( jakartaDataRepository && !quarkusInjection ) { addDefaultConstructor(); } @@ -439,7 +443,8 @@ public class AnnotationMetaEntity extends AnnotationMeta { context.addInjectAnnotation(), context.addNonnullAnnotation(), method != null, - jakartaDataRepository + jakartaDataRepository, + quarkusInjection ) ); return sessionType; diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/DefaultConstructor.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/DefaultConstructor.java index ccee61ea7d..6135b6b98f 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/DefaultConstructor.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/DefaultConstructor.java @@ -59,7 +59,6 @@ public class DefaultConstructor implements MetaAttribute { public String getAttributeDeclarationString() { StringBuilder declaration = new StringBuilder(); declaration.append('\n'); - inject( declaration ); declaration .append("@") .append(annotationMetaEntity.importType("jakarta.persistence.PersistenceUnit")); diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/RepositoryConstructor.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/RepositoryConstructor.java index 978e8bd8cc..0d8d3d7fe2 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/RepositoryConstructor.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/RepositoryConstructor.java @@ -27,6 +27,7 @@ public class RepositoryConstructor implements MetaAttribute { private final boolean addNonnullAnnotation; private final boolean addOverrideAnnotation; private final boolean dataRepository; + private final boolean quarkusInjection; public RepositoryConstructor( Metamodel annotationMetaEntity, @@ -38,7 +39,8 @@ public class RepositoryConstructor implements MetaAttribute { boolean addInjectAnnotation, boolean addNonnullAnnotation, boolean addOverrideAnnotation, - boolean dataRepository) { + boolean dataRepository, + boolean quarkusInjection) { this.annotationMetaEntity = annotationMetaEntity; this.constructorName = constructorName; this.methodName = methodName; @@ -49,6 +51,7 @@ public class RepositoryConstructor implements MetaAttribute { this.addNonnullAnnotation = addNonnullAnnotation; this.addOverrideAnnotation = addOverrideAnnotation; this.dataRepository = dataRepository; + this.quarkusInjection = quarkusInjection; } @Override @@ -86,7 +89,7 @@ public class RepositoryConstructor implements MetaAttribute { .append(constructorName) .append("("); notNull( declaration ); -// named( declaration ); + qualifier( declaration ); declaration .append(annotationMetaEntity.importType(sessionTypeName)) .append(" ") @@ -117,22 +120,24 @@ public class RepositoryConstructor implements MetaAttribute { return declaration.toString(); } -// private void named(StringBuilder declaration) { -// if ( addInjectAnnotation && !dataRepository && dataStore != null ) { -// declaration -// .append('@') -// .append(annotationMetaEntity.importType("jakarta.inject.Named")) -// .append("(\"") -// .append(dataStore) -// .append("\") "); -// } -// } + private void qualifier(StringBuilder declaration) { + if ( addInjectAnnotation && quarkusInjection && dataStore != null ) { + declaration + .append('@') + .append(annotationMetaEntity.importType("io.quarkus.hibernate.orm.PersistenceUnit")) + .append("(\"") + .append(dataStore) + .append("\") "); + } + } private void inject(StringBuilder declaration) { // Jakarta Data repositories are instantiated // via the default constructor, so in that - // case, this one is just for testing - if ( addInjectAnnotation && !dataRepository ) { + // case, this one is just for testing, unless + // we are in Quarkus where we can use + // constructor injection + if ( addInjectAnnotation && (!dataRepository || quarkusInjection) ) { declaration .append('@') .append(annotationMetaEntity.importType("jakarta.inject.Inject"))