Skip to content

Commit 6071f2c

Browse files
committed
fix
1 parent 09ef32e commit 6071f2c

File tree

7 files changed

+187
-67
lines changed

7 files changed

+187
-67
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/impl/MutinySessionImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public <R> MutationQuery createQuery(CriteriaDelete<R> criteriaDelete) {
173173

174174
@Override
175175
public <R> Query<R> createNamedQuery(String queryName) {
176-
return new MutinyQueryImpl<>( delegate.createReactiveNamedQuery( queryName, null ), factory );
176+
return new MutinyQueryImpl<>( delegate.createReactiveNamedQuery( queryName ), factory );
177177
}
178178

179179
@Override

hibernate-reactive-core/src/main/java/org/hibernate/reactive/query/sql/internal/ReactiveNativeQueryImpl.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.hibernate.graph.GraphSemantic;
2727
import org.hibernate.graph.RootGraph;
2828
import org.hibernate.graph.spi.RootGraphImplementor;
29-
import org.hibernate.internal.AbstractSharedSessionContract;
3029
import org.hibernate.metamodel.model.domain.BasicDomainType;
3130
import org.hibernate.query.BindableType;
3231
import org.hibernate.query.Order;
@@ -64,8 +63,18 @@ public class ReactiveNativeQueryImpl<R> extends NativeQueryImpl<R>
6463

6564
private final ReactiveAbstractSelectionQuery<R> selectionQueryDelegate;
6665

67-
public ReactiveNativeQueryImpl(String memento, SharedSessionContractImplementor session) {
68-
super( memento, session );
66+
public ReactiveNativeQueryImpl(String sql, SharedSessionContractImplementor session) {
67+
super( sql, null, session );
68+
this.selectionQueryDelegate = createSelectionQueryDelegate( session );
69+
}
70+
71+
public ReactiveNativeQueryImpl(String sql, Class<R> resultClass, SharedSessionContractImplementor session) {
72+
super( sql, resultClass, session );
73+
this.selectionQueryDelegate = createSelectionQueryDelegate( session );
74+
}
75+
76+
public ReactiveNativeQueryImpl(String sql, NamedResultSetMappingMemento resultSetMappingMemento, Class<R> resultClass, SharedSessionContractImplementor session) {
77+
super( sql, resultSetMappingMemento, resultClass, session);
6978
this.selectionQueryDelegate = createSelectionQueryDelegate( session );
7079
}
7180

@@ -90,14 +99,6 @@ public ReactiveNativeQueryImpl(
9099
this.selectionQueryDelegate = createSelectionQueryDelegate( session );
91100
}
92101

93-
public ReactiveNativeQueryImpl(
94-
String sqlString,
95-
NamedResultSetMappingMemento resultSetMappingMemento,
96-
AbstractSharedSessionContract session) {
97-
super( sqlString, resultSetMappingMemento, session );
98-
this.selectionQueryDelegate = createSelectionQueryDelegate( session );
99-
}
100-
101102
// Convenient for passing parameters to ReactiveAbstractSelectionQuery using method reference
102103
private <T> T getNull() {
103104
return null;

hibernate-reactive-core/src/main/java/org/hibernate/reactive/session/ReactiveQueryProducer.java

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public interface ReactiveQueryProducer extends ReactiveConnectionSupplier {
5757

5858
<R> ReactiveQuery<R> createReactiveQuery(String queryString, Class<R> resultType);
5959

60+
<R> ReactiveQueryImplementor<R> createReactiveNamedQuery(String queryString);
61+
6062
<R> ReactiveQueryImplementor<R> createReactiveNamedQuery(String queryString, Class<R> resultType);
6163

6264
<R> ReactiveNativeQuery<R> createReactiveNativeQuery(String sqlString);

hibernate-reactive-core/src/main/java/org/hibernate/reactive/session/impl/ReactiveSessionImpl.java

+93-28
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,16 @@
7272
import org.hibernate.proxy.HibernateProxy;
7373
import org.hibernate.proxy.LazyInitializer;
7474
import org.hibernate.query.IllegalMutationQueryException;
75+
import org.hibernate.query.UnknownNamedQueryException;
7576
import org.hibernate.query.criteria.JpaCriteriaInsert;
7677
import org.hibernate.query.hql.spi.SqmQueryImplementor;
7778
import org.hibernate.query.named.NamedResultSetMappingMemento;
7879
import org.hibernate.query.spi.HqlInterpretation;
7980
import org.hibernate.query.spi.QueryImplementor;
81+
import org.hibernate.query.sql.spi.NamedNativeQueryMemento;
8082
import org.hibernate.query.sql.spi.NativeQueryImplementor;
8183
import org.hibernate.query.sqm.internal.SqmUtil;
84+
import org.hibernate.query.sqm.spi.NamedSqmQueryMemento;
8285
import org.hibernate.query.sqm.tree.SqmStatement;
8386
import org.hibernate.query.sqm.tree.delete.SqmDeleteStatement;
8487
import org.hibernate.query.sqm.tree.insert.SqmInsertStatement;
@@ -131,7 +134,6 @@
131134
import static org.hibernate.engine.spi.NaturalIdResolutions.INVALID_NATURAL_ID_REFERENCE;
132135
import static org.hibernate.event.spi.LoadEventListener.IMMEDIATE_LOAD;
133136
import static org.hibernate.internal.util.StringHelper.isEmpty;
134-
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
135137
import static org.hibernate.proxy.HibernateProxy.extractLazyInitializer;
136138
import static org.hibernate.reactive.common.InternalStateAssertions.assertUseOnEventLoop;
137139
import static org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister.forceInitialize;
@@ -389,7 +391,7 @@ public <T> ReactiveNativeQueryImplementor<T> createReactiveNativeQuery(String sq
389391
delayedAfterCompletion();
390392

391393
try {
392-
final ReactiveNativeQueryImpl<T> query = new ReactiveNativeQueryImpl<>( sqlString, this );
394+
final ReactiveNativeQueryImpl<T> query = new ReactiveNativeQueryImpl<>( sqlString, null, this );
393395
if ( isEmpty( query.getComment() ) ) {
394396
query.setComment( "dynamic native SQL query" );
395397
}
@@ -445,32 +447,34 @@ public <R> ReactiveNativeQuery<R> createReactiveNativeQuery(String sqlString, Cl
445447
}
446448
}
447449

450+
@Override
451+
public <R> ReactiveNativeQuery<R> createReactiveNativeQuery(String sqlString, String resultSetMappingName, Class<R> resultClass) {
452+
final ReactiveNativeQuery<R> query = createReactiveNativeQuery( sqlString, resultSetMappingName );
453+
if ( Tuple.class.equals( resultClass ) ) {
454+
query.setTupleTransformer( new NativeQueryTupleTransformer() );
455+
}
456+
return query;
457+
}
458+
448459
@Override
449460
public <R> ReactiveNativeQuery<R> createReactiveNativeQuery(String sqlString, String resultSetMappingName) {
461+
if ( isEmpty( resultSetMappingName ) ) {
462+
throw new IllegalArgumentException( "Result set mapping name was not specified" );
463+
}
464+
450465
checkOpen();
451466
pulseTransactionCoordinator();
452467
delayedAfterCompletion();
453468

454469
try {
455-
return isNotEmpty( resultSetMappingName )
456-
? new ReactiveNativeQueryImpl<>( sqlString, getResultSetMappingMemento( resultSetMappingName ), this )
457-
: new ReactiveNativeQueryImpl<>( sqlString, this );
470+
return new ReactiveNativeQueryImpl<>( sqlString, getResultSetMappingMemento( resultSetMappingName ), null, this );
458471
//TODO: why no applyQuerySettingsAndHints( query ); ???
459472
}
460473
catch (RuntimeException he) {
461474
throw getExceptionConverter().convert( he );
462475
}
463476
}
464477

465-
@Override
466-
public <R> ReactiveNativeQuery<R> createReactiveNativeQuery(String sqlString, String resultSetMappingName, Class<R> resultClass) {
467-
final ReactiveNativeQuery<R> query = createReactiveNativeQuery( sqlString, resultSetMappingName );
468-
if ( Tuple.class.equals( resultClass ) ) {
469-
query.setTupleTransformer( new NativeQueryTupleTransformer() );
470-
}
471-
return query;
472-
}
473-
474478
@Override
475479
public <R> ReactiveSelectionQuery<R> createReactiveSelectionQuery(String hqlString, Class<R> resultType) {
476480
return interpretAndCreateSelectionQuery( hqlString, resultType );
@@ -503,9 +507,78 @@ private <R> ReactiveSelectionQuery<R> createSelectionQuery(String hql, Class<R>
503507
return query;
504508
}
505509

510+
@Override
511+
public <R> ReactiveQueryImplementor<R> createReactiveNamedQuery(String name) {
512+
checksBeforeQueryCreation();
513+
try {
514+
return (ReactiveQueryImplementor<R>) buildNamedQuery(
515+
name,
516+
this::createSqmQueryImplementor,
517+
this::createNativeQueryImplementor
518+
);
519+
}
520+
catch (RuntimeException e) {
521+
throw convertNamedQueryException( e );
522+
}
523+
}
524+
506525
@Override
507526
public <R> ReactiveQueryImplementor<R> createReactiveNamedQuery(String name, Class<R> resultType) {
508-
return (ReactiveQueryImplementor<R>) buildNamedQuery( name, resultType );
527+
checksBeforeQueryCreation();
528+
if ( resultType == null ) {
529+
throw new IllegalArgumentException( "Result class is null" );
530+
}
531+
try {
532+
return buildNamedQuery(
533+
name,
534+
memento -> createReactiveSqmQueryImplementor( resultType, memento ),
535+
memento -> createReactiveNativeQueryImplementor( resultType, memento )
536+
);
537+
}
538+
catch (RuntimeException e) {
539+
throw convertNamedQueryException( e );
540+
}
541+
}
542+
543+
private void checksBeforeQueryCreation() {
544+
checkOpen();
545+
checkTransactionSynchStatus();
546+
}
547+
548+
protected <T> ReactiveNativeQueryImpl<T> createReactiveNativeQueryImplementor(Class<T> resultType, NamedNativeQueryMemento<?> memento) {
549+
final NativeQueryImplementor<T> query = memento.toQuery(this, resultType );
550+
if ( isEmpty( query.getComment() ) ) {
551+
query.setComment( "dynamic native SQL query" );
552+
}
553+
applyQuerySettingsAndHints( query );
554+
return (ReactiveNativeQueryImpl<T>) query;
555+
}
556+
557+
protected <T> ReactiveQuerySqmImpl<T> createReactiveSqmQueryImplementor(Class<T> resultType, NamedSqmQueryMemento<?> memento) {
558+
final SqmQueryImplementor<T> query = memento.toQuery( this, resultType );
559+
if ( isEmpty( query.getComment() ) ) {
560+
query.setComment( "dynamic query" );
561+
}
562+
applyQuerySettingsAndHints( query );
563+
if ( memento.getLockOptions() != null ) {
564+
query.setLockOptions( memento.getLockOptions() );
565+
}
566+
return (ReactiveQuerySqmImpl<T>) query;
567+
}
568+
569+
private RuntimeException convertNamedQueryException(RuntimeException e) {
570+
if ( e instanceof UnknownNamedQueryException ) {
571+
// JPA expects this to mark the transaction for rollback only
572+
getTransactionCoordinator().getTransactionDriverControl().markRollbackOnly();
573+
// it also expects an IllegalArgumentException, so wrap UnknownNamedQueryException
574+
return new IllegalArgumentException( e.getMessage(), e );
575+
}
576+
else if ( e instanceof IllegalArgumentException ) {
577+
return e;
578+
}
579+
else {
580+
return getExceptionConverter().convert( e );
581+
}
509582
}
510583

511584
@Override
@@ -590,7 +663,7 @@ public <R> ReactiveNativeQuery<R> createReactiveNativeQuery(String queryString,
590663
delayedAfterCompletion();
591664

592665
try {
593-
final ReactiveNativeQueryImpl<R> query = new ReactiveNativeQueryImpl<>( queryString, this );
666+
final ReactiveNativeQueryImpl<R> query = new ReactiveNativeQueryImpl<>( queryString, null, this );
594667
addAffectedEntities( affectedEntities, query );
595668
if ( isEmpty( query.getComment() ) ) {
596669
query.setComment( "dynamic native SQL query" );
@@ -620,12 +693,11 @@ public <R> ReactiveNativeQueryImpl<R> createReactiveNativeQuery(String queryStri
620693
checkOpen();
621694
pulseTransactionCoordinator();
622695
delayedAfterCompletion();
623-
696+
// Should we throw an exception?
697+
NamedResultSetMappingMemento memento = resultSetMapping == null ? null : getResultSetMappingMemento( resultSetMapping.getName() );
624698
try {
625699
// Same approach as AbstractSharedSessionContract#createNativeQuery(String, String)
626-
final ReactiveNativeQueryImpl<R> nativeQuery = resultSetMapping != null
627-
? new ReactiveNativeQueryImpl<>( queryString, getResultSetMappingMemento( resultSetMapping.getName() ), this )
628-
: new ReactiveNativeQueryImpl<>( queryString, this );
700+
final ReactiveNativeQueryImpl<R> nativeQuery = new ReactiveNativeQueryImpl<>( queryString, memento, null, this );
629701
applyQuerySettingsAndHints( nativeQuery );
630702
return nativeQuery;
631703
}
@@ -650,20 +722,13 @@ public <T> ResultSetMapping<T> getResultSetMapping(Class<T> resultType, String m
650722
if ( mapping == null ) {
651723
throw new IllegalArgumentException( "result set mapping does not exist: " + mappingName );
652724
}
653-
//
654-
// ResultSetMappingImpl resultSetMapping = new ResultSetMappingImpl( "impl" );
655-
// if ( resultType != null ) {
656-
// Class<?> mappedResultType = resultSetMapping.;
657-
// if ( !resultType.equals( mappedResultType ) ) {
658-
// throw new IllegalArgumentException( "incorrect result type for result set mapping: " + mappingName + " has type " + mappedResultType.getName() );
659-
// }
660-
// }
661725

662726
return new ResultSetMapping<>() {
663727
@Override
664728
public String getName() {
665729
return mappingName;
666730
}
731+
667732
@Override
668733
public Class<T> getResultType() {
669734
return resultType;

hibernate-reactive-core/src/main/java/org/hibernate/reactive/session/impl/ReactiveStatelessSessionImpl.java

+62-11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.hibernate.proxy.HibernateProxy;
4646
import org.hibernate.proxy.LazyInitializer;
4747
import org.hibernate.query.IllegalMutationQueryException;
48+
import org.hibernate.query.UnknownNamedQueryException;
4849
import org.hibernate.query.criteria.JpaCriteriaInsert;
4950
import org.hibernate.query.hql.spi.SqmQueryImplementor;
5051
import org.hibernate.query.named.NamedResultSetMappingMemento;
@@ -848,7 +849,7 @@ public <R> ReactiveNativeQueryImplementor<R> createReactiveNativeQuery(String sq
848849
delayedAfterCompletion();
849850

850851
try {
851-
ReactiveNativeQueryImpl<R> query = new ReactiveNativeQueryImpl<>( sqlString, this);
852+
ReactiveNativeQueryImpl<R> query = new ReactiveNativeQueryImpl<>( sqlString, null, this);
852853

853854
if ( isEmpty( query.getComment() ) ) {
854855
query.setComment( "dynamic native SQL query" );
@@ -905,7 +906,7 @@ public <R> ReactiveNativeQuery<R> createReactiveNativeQuery(String sqlString, St
905906
if ( resultSetMappingMemento == null ) {
906907
throw new HibernateException( "Could not resolve specified result-set mapping name : " + resultSetMappingName );
907908
}
908-
return new ReactiveNativeQueryImpl<>( sqlString, resultSetMappingMemento, this );
909+
return new ReactiveNativeQueryImpl<>( sqlString, resultSetMappingMemento, null, this );
909910
}
910911
else {
911912
return new ReactiveNativeQueryImpl<>( sqlString, this );
@@ -1022,11 +1023,64 @@ public <R> ReactiveMutationQuery<R> createNamedReactiveMutationQuery(String quer
10221023
);
10231024
}
10241025

1026+
@Override
1027+
public <R> ReactiveQueryImplementor<R> createReactiveNamedQuery(String queryName) {
1028+
checksBeforeQueryCreation();
1029+
try {
1030+
return (ReactiveQueryImplementor<R>) buildNamedQuery(
1031+
queryName,
1032+
this::createSqmQueryImplementor,
1033+
this::createNativeQueryImplementor
1034+
);
1035+
}
1036+
catch (RuntimeException e) {
1037+
throw convertNamedQueryException( e );
1038+
}
1039+
}
1040+
1041+
@Override
1042+
public <R> ReactiveQueryImplementor<R> createReactiveNamedQuery(String queryName, Class<R> resultType) {
1043+
checksBeforeQueryCreation();
1044+
if ( resultType == null ) {
1045+
throw new IllegalArgumentException( "Result class is null" );
1046+
}
1047+
try {
1048+
return (ReactiveQueryImplementor<R>) buildNamedQuery(
1049+
queryName,
1050+
memento -> createSqmQueryImplementor( resultType, memento ),
1051+
memento -> createNativeQueryImplementor( resultType, memento )
1052+
);
1053+
}
1054+
catch (RuntimeException e) {
1055+
throw convertNamedQueryException( e );
1056+
}
1057+
}
1058+
1059+
private RuntimeException convertNamedQueryException(RuntimeException e) {
1060+
if ( e instanceof UnknownNamedQueryException ) {
1061+
// JPA expects this to mark the transaction for rollback only
1062+
getTransactionCoordinator().getTransactionDriverControl().markRollbackOnly();
1063+
// it also expects an IllegalArgumentException, so wrap UnknownNamedQueryException
1064+
return new IllegalArgumentException( e.getMessage(), e );
1065+
}
1066+
else if ( e instanceof IllegalArgumentException ) {
1067+
return e;
1068+
}
1069+
else {
1070+
return getExceptionConverter().convert( e );
1071+
}
1072+
}
1073+
10251074
@Override
10261075
public <R> ReactiveSelectionQuery<R> createNamedReactiveSelectionQuery(String queryName, Class<R> expectedResultType) {
10271076
return (ReactiveSelectionQuery<R>) createNamedSelectionQuery( queryName , expectedResultType );
10281077
}
10291078

1079+
private void checksBeforeQueryCreation() {
1080+
checkOpen();
1081+
checkTransactionSynchStatus();
1082+
}
1083+
10301084
@Override
10311085
public <R> ReactiveMutationQuery<R> createNativeReactiveMutationQuery(String sqlString) {
10321086
final ReactiveNativeQueryImplementor<R> query = createReactiveNativeQuery( sqlString );
@@ -1036,11 +1090,6 @@ public <R> ReactiveMutationQuery<R> createNativeReactiveMutationQuery(String sql
10361090
return query;
10371091
}
10381092

1039-
@Override
1040-
public <R> ReactiveQueryImplementor<R> createReactiveNamedQuery(String queryName, Class<R> resultType) {
1041-
return (ReactiveQueryImplementor<R>) buildNamedQuery( queryName, resultType );
1042-
}
1043-
10441093
@Override
10451094
public <R> ReactiveNativeQuery<R> createReactiveNativeQuery(String queryString, AffectedEntities affectedEntities) {
10461095
checkOpen();
@@ -1090,11 +1139,13 @@ public <R> ReactiveNativeQueryImpl<R> createReactiveNativeQuery(String queryStri
10901139
pulseTransactionCoordinator();
10911140
delayedAfterCompletion();
10921141

1142+
if ( resultSetMapping == null ) {
1143+
throw new IllegalArgumentException( "Result set mapping was not specified" );
1144+
}
1145+
10931146
try {
1094-
// Same approach as AbstractSharedSessionContract#createNativeQuery(String, String)
1095-
final ReactiveNativeQueryImpl<R> nativeQuery = resultSetMapping != null
1096-
? new ReactiveNativeQueryImpl<>( queryString, getResultSetMappingMemento( resultSetMapping.getName() ), this )
1097-
: new ReactiveNativeQueryImpl<>( queryString, this );
1147+
final NamedResultSetMappingMemento memento = getResultSetMappingMemento( resultSetMapping.getName() );
1148+
final ReactiveNativeQueryImpl<R> nativeQuery = new ReactiveNativeQueryImpl<>( queryString, memento, null, this );
10981149
applyQuerySettingsAndHints( nativeQuery );
10991150
return nativeQuery;
11001151
}

0 commit comments

Comments
 (0)