HHH-17772 interpret @Repository(dataSource) as a CDI @Named qualifier

This is sort of the simplest possible thing to do.
This commit is contained in:
Gavin King 2024-02-23 20:04:37 +01:00
parent 484fcb2984
commit 1e32439b3c
2 changed files with 28 additions and 0 deletions

View File

@ -336,6 +336,17 @@ public class AnnotationMetaEntity extends AnnotationMeta {
initialized = true;
}
private @Nullable String dataStore() {
final AnnotationMirror repo = getAnnotationMirror( element, JD_REPOSITORY );
if ( repo != null ) {
final String dataStore = (String) getAnnotationValue( repo, "dataStore" );
if ( dataStore != null && !dataStore.isEmpty() ) {
return dataStore;
}
}
return null;
}
private void findSessionGetter(TypeElement type) {
if ( !hasAnnotation( type, Constants.ENTITY )
&& !hasAnnotation( type, Constants.MAPPED_SUPERCLASS )
@ -379,6 +390,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
name,
sessionType,
sessionVariableName,
dataStore(),
context.addInjectAnnotation(),
context.addNonnullAnnotation(),
method != null

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.jpamodelgen.annotation;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.model.Metamodel;
import org.hibernate.jpamodelgen.util.Constants;
@ -19,6 +20,7 @@ public class DaoConstructor implements MetaAttribute {
private final String methodName;
private final String sessionTypeName;
private final String sessionVariableName;
private final @Nullable String dataStore;
private final boolean addInjectAnnotation;
private final boolean addNonnullAnnotation;
private final boolean addOverrideAnnotation;
@ -29,6 +31,7 @@ public class DaoConstructor implements MetaAttribute {
String methodName,
String sessionTypeName,
String sessionVariableName,
@Nullable String dataStore,
boolean addInjectAnnotation,
boolean addNonnullAnnotation,
boolean addOverrideAnnotation) {
@ -37,6 +40,7 @@ public class DaoConstructor implements MetaAttribute {
this.methodName = methodName;
this.sessionTypeName = sessionTypeName;
this.sessionVariableName = sessionVariableName;
this.dataStore = dataStore;
this.addInjectAnnotation = addInjectAnnotation;
this.addNonnullAnnotation = addNonnullAnnotation;
this.addOverrideAnnotation = addOverrideAnnotation;
@ -70,6 +74,7 @@ public class DaoConstructor implements MetaAttribute {
.append(constructorName)
.append("(");
notNull( declaration );
named( declaration );
declaration
.append(annotationMetaEntity.importType(sessionTypeName))
.append(" ")
@ -100,6 +105,17 @@ public class DaoConstructor implements MetaAttribute {
return declaration.toString();
}
private void named(StringBuilder declaration) {
if ( addInjectAnnotation && dataStore != null ) {
declaration
.append("@")
.append(annotationMetaEntity.importType("jakarta.inject.Named"))
.append("(\"")
.append(dataStore)
.append("\") ");
}
}
private void inject(StringBuilder declaration) {
if ( addInjectAnnotation ) {
declaration