Skip to content

Commit 70f9060

Browse files
committed
when CDI is missing, do still add an @Inject annotation on the constructor
at least when jakarta.inject is available Signed-off-by: Gavin King <[email protected]>
1 parent c945b16 commit 70f9060

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,28 @@ else if ( element.getKind() == ElementKind.INTERFACE
535535
sessionType = HIB_STATELESS_SESSION;
536536
addDaoConstructor( null );
537537
}
538-
if ( jakartaDataRepository && !quarkusInjection
539-
&& context.addDependentAnnotation() ) {
538+
if ( needsDefaultConstructor() ) {
540539
addDefaultConstructor();
541540
}
542541
}
543542
}
544543

544+
/**
545+
* For usage with CDI, but outside Quarkus, Jakarta Data
546+
* repositories use {@code @PersistenceUnit} to obtain an
547+
* {@code EntityManagerFactory} via field injection. So in
548+
* that case we will need a {@link DefaultConstructor default
549+
* constructor}. We don't do this in Quarkus, because there
550+
* we can just inject the {@code StatelessSession} directly,
551+
* and so in Quarkus we don't need the default constructor
552+
* at all.
553+
*/
554+
boolean needsDefaultConstructor() {
555+
return jakartaDataRepository
556+
&& !quarkusInjection
557+
&& context.addDependentAnnotation();
558+
}
559+
545560
private @Nullable ExecutableElement findSessionGetter(TypeElement type) {
546561
if ( !hasAnnotation( type, ENTITY, MAPPED_SUPERCLASS, EMBEDDABLE )
547562
|| isPanacheType( type ) ) {

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @author Gavin King
1818
*/
1919
public class RepositoryConstructor implements MetaAttribute {
20-
private final Metamodel annotationMetaEntity;
20+
private final AnnotationMetaEntity annotationMetaEntity;
2121
private final String constructorName;
2222
private final String methodName;
2323
private final String sessionTypeName;
@@ -30,7 +30,7 @@ public class RepositoryConstructor implements MetaAttribute {
3030
private final boolean quarkusInjection;
3131

3232
public RepositoryConstructor(
33-
Metamodel annotationMetaEntity,
33+
AnnotationMetaEntity annotationMetaEntity,
3434
String constructorName,
3535
String methodName,
3636
String sessionTypeName,
@@ -135,6 +135,11 @@ public String getAttributeDeclarationString() {
135135
return declaration.toString();
136136
}
137137

138+
/**
139+
* In Quarkus we use the Quarkus-specific {@code @PersistenceUnit}
140+
* CDI qualifier annotation to inject the {@code StatelessSession}
141+
* directly.
142+
*/
138143
private void qualifier(StringBuilder declaration) {
139144
if ( addInjectAnnotation && quarkusInjection && dataStore != null ) {
140145
declaration
@@ -146,13 +151,23 @@ private void qualifier(StringBuilder declaration) {
146151
}
147152
}
148153

154+
/**
155+
* In Quarkus we inject the {@code StatelessSession}
156+
* directly via the constructor. But this doesn't work
157+
* in other CDI implementations, where we need to use
158+
* the JPA {@code @PersistenceUnit} annotation for
159+
* field injection of an {@code EntityManager}. In
160+
* that case, CDI will instantiate the repository via
161+
* a {@link DefaultConstructor default constructor},
162+
* so we don't need to mark this one {@code @Inject}.
163+
*/
149164
private void inject(StringBuilder declaration) {
150165
// Jakarta Data repositories are instantiated
151166
// via the default constructor, so in that
152167
// case, this one is just for testing, unless
153168
// we are in Quarkus where we can use
154169
// constructor injection
155-
if ( addInjectAnnotation && (!dataRepository || quarkusInjection) ) {
170+
if ( addInjectAnnotation && !annotationMetaEntity.needsDefaultConstructor() ) {
156171
declaration
157172
.append('@')
158173
.append(annotationMetaEntity.importType("jakarta.inject.Inject"))

0 commit comments

Comments
 (0)