Skip to content

Commit 5df154a

Browse files
committed
HHH-19531 Test use of session proxies with SelectionSpecification/MutationSpecification
1 parent 2daccac commit 5df154a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/query/dynamic/SimpleQuerySpecificationTests.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import jakarta.persistence.criteria.CriteriaQuery;
99

1010
import org.hibernate.SessionFactory;
11+
import org.hibernate.engine.spi.SessionLazyDelegator;
12+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1113
import org.hibernate.query.IllegalMutationQueryException;
1214
import org.hibernate.query.IllegalSelectQueryException;
1315
import org.hibernate.query.Order;
@@ -19,6 +21,7 @@
1921
import org.hibernate.query.restriction.Restriction;
2022
import org.hibernate.testing.jdbc.SQLStatementInspector;
2123
import org.hibernate.testing.orm.junit.DomainModel;
24+
import org.hibernate.testing.orm.junit.JiraKey;
2225
import org.hibernate.testing.orm.junit.SessionFactoryScope;
2326
import org.junit.jupiter.api.Test;
2427

@@ -160,6 +163,46 @@ void testSimpleMutationRestriction(SessionFactoryScope factoryScope) {
160163
assertThat( sqlCollector.getSqlQueries().get( 0 ) ).contains( " where be1_0.position between ? and ?" );
161164
}
162165

166+
@Test
167+
@JiraKey("HHH-19531")
168+
void testSelectionOnSessionProxy(SessionFactoryScope factoryScope) {
169+
final SQLStatementInspector sqlCollector = factoryScope.getCollectingStatementInspector();
170+
171+
factoryScope.inTransaction( (session) -> {
172+
var sessionProxy = new SessionLazyDelegator( () -> session );
173+
// The test only makes sense if this is true. It currently is, but who knows what the future has in store for us.
174+
//noinspection ConstantValue
175+
assert !(sessionProxy instanceof SharedSessionContractImplementor);
176+
177+
sqlCollector.clear();
178+
SelectionSpecification.create( BasicEntity.class, "from BasicEntity" )
179+
.createQuery( sessionProxy )
180+
.list();
181+
} );
182+
183+
assertThat( sqlCollector.getSqlQueries() ).hasSize( 1 );
184+
}
185+
186+
@Test
187+
@JiraKey("HHH-19531")
188+
void testMutationOnSessionProxy(SessionFactoryScope factoryScope) {
189+
final SQLStatementInspector sqlCollector = factoryScope.getCollectingStatementInspector();
190+
191+
factoryScope.inTransaction( (session) -> {
192+
var sessionProxy = new SessionLazyDelegator( () -> session );
193+
// The test only makes sense if this is true. It currently is, but who knows what the future has in store for us.
194+
//noinspection ConstantValue
195+
assert !(sessionProxy instanceof SharedSessionContractImplementor);
196+
197+
sqlCollector.clear();
198+
MutationSpecification.create( BasicEntity.class, "delete BasicEntity" )
199+
.createQuery( sessionProxy )
200+
.executeUpdate();
201+
} );
202+
203+
assertThat( sqlCollector.getSqlQueries() ).hasSize( 1 );
204+
}
205+
163206
@Test
164207
void testSimpleMutationRestrictionAsReference(SessionFactoryScope factoryScope) {
165208
final SQLStatementInspector sqlCollector = factoryScope.getCollectingStatementInspector();

0 commit comments

Comments
 (0)