Skip to content

Commit a4adc14

Browse files
committed
fix
1 parent 429f6c2 commit a4adc14

File tree

7 files changed

+192
-61
lines changed

7 files changed

+192
-61
lines changed

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 11 deletions
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;
@@ -65,8 +64,18 @@ public class ReactiveNativeQueryImpl<R> extends NativeQueryImpl<R>
6564

6665
private final ReactiveAbstractSelectionQuery<R> selectionQueryDelegate;
6766

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

@@ -91,14 +100,6 @@ public ReactiveNativeQueryImpl(
91100
this.selectionQueryDelegate = createSelectionQueryDelegate( session );
92101
}
93102

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

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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 84 additions & 19 deletions
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;
@@ -387,7 +389,7 @@ public <T> ReactiveNativeQueryImplementor<T> createReactiveNativeQuery(String sq
387389
delayedAfterCompletion();
388390

389391
try {
390-
final ReactiveNativeQueryImpl<T> query = new ReactiveNativeQueryImpl<>( sqlString, this );
392+
final ReactiveNativeQueryImpl<T> query = new ReactiveNativeQueryImpl<>( sqlString, mthis );
391393
if ( isEmpty( query.getComment() ) ) {
392394
query.setComment( "dynamic native SQL query" );
393395
}
@@ -443,14 +445,16 @@ public <R> ReactiveNativeQuery<R> createReactiveNativeQuery(String sqlString, Cl
443445

444446
@Override @Deprecated(forRemoval = true)
445447
public <R> ReactiveNativeQuery<R> createReactiveNativeQuery(String sqlString, String resultSetMappingName) {
448+
if ( isEmpty( resultSetMappingName ) ) {
449+
throw new IllegalArgumentException( "Result set mapping name was not specified" );
450+
}
451+
446452
checkOpen();
447453
pulseTransactionCoordinator();
448454
delayedAfterCompletion();
449455

450456
try {
451-
return isNotEmpty( resultSetMappingName )
452-
? new ReactiveNativeQueryImpl<>( sqlString, getResultSetMappingMemento( resultSetMappingName ), this )
453-
: new ReactiveNativeQueryImpl<>( sqlString, this );
457+
return new ReactiveNativeQueryImpl<>( sqlString, getResultSetMappingMemento( resultSetMappingName ), null, this );
454458
//TODO: why no applyQuerySettingsAndHints( query ); ???
455459
}
456460
catch (RuntimeException he) {
@@ -497,9 +501,78 @@ private <R> ReactiveSelectionQuery<R> createSelectionQuery(String hql, Class<R>
497501
return query;
498502
}
499503

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

505578
@Override
@@ -584,7 +657,7 @@ public <R> ReactiveNativeQuery<R> createReactiveNativeQuery(String queryString,
584657
delayedAfterCompletion();
585658

586659
try {
587-
final ReactiveNativeQueryImpl<R> query = new ReactiveNativeQueryImpl<>( queryString, this );
660+
final ReactiveNativeQueryImpl<R> query = new ReactiveNativeQueryImpl<>( queryString, null, this );
588661
addAffectedEntities( affectedEntities, query );
589662
if ( isEmpty( query.getComment() ) ) {
590663
query.setComment( "dynamic native SQL query" );
@@ -622,12 +695,11 @@ public <R> ReactiveNativeQueryImpl<R> createReactiveNativeQuery(String queryStri
622695
checkOpen();
623696
pulseTransactionCoordinator();
624697
delayedAfterCompletion();
625-
698+
// Should we throw an exception?
699+
NamedResultSetMappingMemento memento = resultSetMapping == null ? null : getResultSetMappingMemento( resultSetMapping.getName() );
626700
try {
627701
// Same approach as AbstractSharedSessionContract#createNativeQuery(String, String)
628-
final ReactiveNativeQueryImpl<R> nativeQuery = resultSetMapping != null
629-
? new ReactiveNativeQueryImpl<>( queryString, getResultSetMappingMemento( resultSetMapping.getName() ), this )
630-
: new ReactiveNativeQueryImpl<>( queryString, this );
702+
final ReactiveNativeQueryImpl<R> nativeQuery = new ReactiveNativeQueryImpl<>( queryString, memento, null, this );
631703
applyQuerySettingsAndHints( nativeQuery );
632704
return nativeQuery;
633705
}
@@ -652,20 +724,13 @@ public <T> ResultSetMapping<T> getResultSetMapping(Class<T> resultType, String m
652724
if ( mapping == null ) {
653725
throw new IllegalArgumentException( "result set mapping does not exist: " + mappingName );
654726
}
655-
//
656-
// ResultSetMappingImpl resultSetMapping = new ResultSetMappingImpl( "impl" );
657-
// if ( resultType != null ) {
658-
// Class<?> mappedResultType = resultSetMapping.;
659-
// if ( !resultType.equals( mappedResultType ) ) {
660-
// throw new IllegalArgumentException( "incorrect result type for result set mapping: " + mappingName + " has type " + mappedResultType.getName() );
661-
// }
662-
// }
663727

664728
return new ResultSetMapping<>() {
665729
@Override
666730
public String getName() {
667731
return mappingName;
668732
}
733+
669734
@Override
670735
public Class<T> getResultType() {
671736
return resultType;

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

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.hibernate.cache.spi.access.EntityDataAccess;
2323
import org.hibernate.collection.spi.PersistentCollection;
2424
import org.hibernate.dialect.Dialect;
25+
import org.hibernate.engine.internal.ReactivePersistenceContextAdapter;
2526
import org.hibernate.engine.spi.EntityKey;
2627
import org.hibernate.engine.spi.LoadQueryInfluencers;
2728
import org.hibernate.engine.spi.PersistenceContext;
@@ -47,10 +48,12 @@
4748
import org.hibernate.proxy.HibernateProxy;
4849
import org.hibernate.proxy.LazyInitializer;
4950
import org.hibernate.query.IllegalMutationQueryException;
51+
import org.hibernate.query.UnknownNamedQueryException;
5052
import org.hibernate.query.criteria.JpaCriteriaInsert;
5153
import org.hibernate.query.criteria.JpaCriteriaQuery;
5254
import org.hibernate.query.criteria.JpaRoot;
5355
import org.hibernate.query.hql.spi.SqmQueryImplementor;
56+
import org.hibernate.query.named.NamedResultSetMappingMemento;
5457
import org.hibernate.query.spi.HqlInterpretation;
5558
import org.hibernate.query.spi.QueryImplementor;
5659
import org.hibernate.query.sql.spi.NativeQueryImplementor;
@@ -64,7 +67,6 @@
6467
import org.hibernate.query.sqm.tree.update.SqmUpdateStatement;
6568
import org.hibernate.reactive.common.AffectedEntities;
6669
import org.hibernate.reactive.common.ResultSetMapping;
67-
import org.hibernate.engine.internal.ReactivePersistenceContextAdapter;
6870
import org.hibernate.reactive.id.ReactiveIdentifierGenerator;
6971
import org.hibernate.reactive.logging.impl.Log;
7072
import org.hibernate.reactive.persister.collection.impl.ReactiveCollectionPersister;
@@ -905,7 +907,7 @@ public <R> ReactiveNativeQueryImplementor<R> createReactiveNativeQuery(String sq
905907
delayedAfterCompletion();
906908

907909
try {
908-
final ReactiveNativeQueryImpl<R> query = new ReactiveNativeQueryImpl<>( sqlString, this );
910+
final ReactiveNativeQueryImpl<R> query = new ReactiveNativeQueryImpl<>( sqlString, null, this);
909911
if ( isEmpty( query.getComment() ) ) {
910912
query.setComment( "dynamic native SQL query" );
911913
}
@@ -956,9 +958,19 @@ public <R> ReactiveNativeQuery<R> createReactiveNativeQuery(String sqlString, St
956958
delayedAfterCompletion();
957959

958960
try {
959-
return isNotEmpty( resultSetMappingName )
960-
? new ReactiveNativeQueryImpl<>( sqlString, getResultSetMappingMemento( resultSetMappingName ), this )
961-
: new ReactiveNativeQueryImpl<>( sqlString, this );
961+
if ( isNotEmpty( resultSetMappingName ) ) {
962+
final NamedResultSetMappingMemento resultSetMappingMemento = getFactory().getQueryEngine()
963+
.getNamedObjectRepository()
964+
.getResultSetMappingMemento( resultSetMappingName );
965+
966+
if ( resultSetMappingMemento == null ) {
967+
throw new HibernateException( "Could not resolve specified result-set mapping name : " + resultSetMappingName );
968+
}
969+
return new ReactiveNativeQueryImpl<>( sqlString, resultSetMappingMemento, null, this );
970+
}
971+
else {
972+
return new ReactiveNativeQueryImpl<>( sqlString, this );
973+
}
962974
//TODO: why no applyQuerySettingsAndHints( query ); ???
963975
}
964976
catch (RuntimeException he) {
@@ -1069,11 +1081,64 @@ public <R> ReactiveMutationQuery<R> createNamedReactiveMutationQuery(String quer
10691081
);
10701082
}
10711083

1084+
@Override
1085+
public <R> ReactiveQueryImplementor<R> createReactiveNamedQuery(String queryName) {
1086+
checksBeforeQueryCreation();
1087+
try {
1088+
return (ReactiveQueryImplementor<R>) buildNamedQuery(
1089+
queryName,
1090+
this::createSqmQueryImplementor,
1091+
this::createNativeQueryImplementor
1092+
);
1093+
}
1094+
catch (RuntimeException e) {
1095+
throw convertNamedQueryException( e );
1096+
}
1097+
}
1098+
1099+
@Override
1100+
public <R> ReactiveQueryImplementor<R> createReactiveNamedQuery(String queryName, Class<R> resultType) {
1101+
checksBeforeQueryCreation();
1102+
if ( resultType == null ) {
1103+
throw new IllegalArgumentException( "Result class is null" );
1104+
}
1105+
try {
1106+
return (ReactiveQueryImplementor<R>) buildNamedQuery(
1107+
queryName,
1108+
memento -> createSqmQueryImplementor( resultType, memento ),
1109+
memento -> createNativeQueryImplementor( resultType, memento )
1110+
);
1111+
}
1112+
catch (RuntimeException e) {
1113+
throw convertNamedQueryException( e );
1114+
}
1115+
}
1116+
1117+
private RuntimeException convertNamedQueryException(RuntimeException e) {
1118+
if ( e instanceof UnknownNamedQueryException ) {
1119+
// JPA expects this to mark the transaction for rollback only
1120+
getTransactionCoordinator().getTransactionDriverControl().markRollbackOnly();
1121+
// it also expects an IllegalArgumentException, so wrap UnknownNamedQueryException
1122+
return new IllegalArgumentException( e.getMessage(), e );
1123+
}
1124+
else if ( e instanceof IllegalArgumentException ) {
1125+
return e;
1126+
}
1127+
else {
1128+
return getExceptionConverter().convert( e );
1129+
}
1130+
}
1131+
10721132
@Override
10731133
public <R> ReactiveSelectionQuery<R> createNamedReactiveSelectionQuery(String queryName, Class<R> expectedResultType) {
10741134
return (ReactiveSelectionQuery<R>) createNamedSelectionQuery( queryName , expectedResultType );
10751135
}
10761136

1137+
private void checksBeforeQueryCreation() {
1138+
checkOpen();
1139+
checkTransactionSynchStatus();
1140+
}
1141+
10771142
@Override
10781143
public <R> ReactiveMutationQuery<R> createNativeReactiveMutationQuery(String sqlString) {
10791144
final ReactiveNativeQueryImplementor<R> query = createReactiveNativeQuery( sqlString );
@@ -1083,11 +1148,6 @@ public <R> ReactiveMutationQuery<R> createNativeReactiveMutationQuery(String sql
10831148
return query;
10841149
}
10851150

1086-
@Override
1087-
public <R> ReactiveQueryImplementor<R> createReactiveNamedQuery(String queryName, Class<R> resultType) {
1088-
return (ReactiveQueryImplementor<R>) buildNamedQuery( queryName, resultType );
1089-
}
1090-
10911151
@Override
10921152
public <R> ReactiveNativeQuery<R> createReactiveNativeQuery(String queryString, AffectedEntities affectedEntities) {
10931153
checkOpen();
@@ -1131,11 +1191,13 @@ public <R> ReactiveNativeQueryImpl<R> createReactiveNativeQuery(String queryStri
11311191
pulseTransactionCoordinator();
11321192
delayedAfterCompletion();
11331193

1194+
if ( resultSetMapping == null ) {
1195+
throw new IllegalArgumentException( "Result set mapping was not specified" );
1196+
}
1197+
11341198
try {
1135-
// Same approach as AbstractSharedSessionContract#createNativeQuery(String, String)
1136-
final ReactiveNativeQueryImpl<R> nativeQuery = resultSetMapping != null
1137-
? new ReactiveNativeQueryImpl<>( queryString, getResultSetMappingMemento( resultSetMapping.getName() ), this )
1138-
: new ReactiveNativeQueryImpl<>( queryString, this );
1199+
final NamedResultSetMappingMemento memento = getResultSetMappingMemento( resultSetMapping.getName() );
1200+
final ReactiveNativeQueryImpl<R> nativeQuery = new ReactiveNativeQueryImpl<>( queryString, memento, null, this );
11391201
applyQuerySettingsAndHints( nativeQuery );
11401202
return nativeQuery;
11411203
}

0 commit comments

Comments
 (0)