Skip to content

Commit 7f5bedd

Browse files
committed
HHH-19606 allow resource accessor method of type StatelessSession in Spring
this is a partial fix: need to consider other session types eventually
1 parent 4e7760a commit 7f5bedd

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -786,10 +786,10 @@ else if ( element.getKind() == ElementKind.INTERFACE
786786
}
787787
if ( !repository && jakartaDataRepository ) {
788788
repository = true;
789-
sessionType = springInjection
790-
? SPRING_STATELESS_SESSION_PROVIDER
791-
: HIB_STATELESS_SESSION;
792-
addDaoConstructor( null );
789+
// Jakarta Data defaults to StatelessSession, not EntityManager
790+
sessionType = HIB_STATELESS_SESSION;
791+
// If it's Spring, we wrap the StatelessSession in ObjectProvider
792+
sessionType = addDaoConstructor( null );
793793
}
794794
if ( needsDefaultConstructor() ) {
795795
addDefaultConstructor();
@@ -895,7 +895,11 @@ private boolean isReactivePanacheType(TypeElement type) {
895895
* it.
896896
*/
897897
private String addDaoConstructor(@Nullable ExecutableElement method) {
898-
final String sessionType = method == null ? this.sessionType : fullReturnType(method);
898+
final String returnType = method == null ? this.sessionType : fullReturnType( method );
899+
final String sessionType =
900+
jakartaDataRepository && springInjection
901+
? SPRING_OBJECT_PROVIDER + '<' + returnType + '>'
902+
: returnType;
899903
final String sessionVariableName = getSessionVariableName( sessionType );
900904
final String name = method == null ? sessionVariableName : method.getSimpleName().toString();
901905

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.hibernate.processor.model.Metamodel;
1010
import org.hibernate.processor.util.Constants;
1111

12+
import static org.hibernate.processor.util.Constants.HIB_STATELESS_SESSION;
1213
import static org.hibernate.processor.util.Constants.INJECT;
1314
import static org.hibernate.processor.util.Constants.NONNULL;
1415

@@ -126,17 +127,29 @@ public String getAttributeDeclarationString() {
126127
.append("public ");
127128
notNull( declaration );
128129
declaration
129-
.append(annotationMetaEntity.importType(sessionTypeName))
130+
.append(annotationMetaEntity.importType(providedSessionType()))
130131
.append(" ")
131132
.append(methodName)
132133
.append("() {")
133134
.append("\n\treturn ")
134-
.append(sessionVariableName)
135+
.append(sessionVariableName);
136+
if ( annotationMetaEntity.isProvidedSessionAccess() ) {
137+
declaration
138+
.append( ".getObject()" );
139+
}
140+
declaration
135141
.append(";\n}");
136142
}
137143
return declaration.toString();
138144
}
139145

146+
private String providedSessionType() {
147+
return annotationMetaEntity.isProvidedSessionAccess()
148+
//TODO: assuming provided sessions are always StatelessSessions for now
149+
? HIB_STATELESS_SESSION
150+
: sessionTypeName;
151+
}
152+
140153
/**
141154
* In Quarkus we use the Quarkus-specific {@code @PersistenceUnit}
142155
* CDI qualifier annotation to inject the {@code StatelessSession}

0 commit comments

Comments
 (0)