HHH-17673 - allow the use of StatementInspector with stateless sessions

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2024-01-23 21:23:43 +01:00 committed by Christian Beikov
parent 34a66fcbfa
commit fc3f975f0b
3 changed files with 49 additions and 2 deletions

View File

@ -8,6 +8,8 @@ package org.hibernate;
import java.sql.Connection;
import org.hibernate.resource.jdbc.spi.StatementInspector;
/**
* Allows creation of a new {@link StatelessSession} with specific options.
*
@ -50,4 +52,13 @@ public interface StatelessSessionBuilder {
* @since 6.4
*/
StatelessSessionBuilder tenantIdentifier(Object tenantIdentifier);
/**
* Applies the given {@link StatementInspector} to the stateless session.
*
* @param statementInspector The StatementInspector to use.
*
* @return {@code this}, for method chaining
*/
StatelessSessionBuilder statementInspector(StatementInspector statementInspector);
}

View File

@ -1469,11 +1469,13 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im
public static class StatelessSessionBuilderImpl implements StatelessSessionBuilder, SessionCreationOptions {
private final SessionFactoryImpl sessionFactory;
private StatementInspector statementInspector;
private Connection connection;
private Object tenantIdentifier;
public StatelessSessionBuilderImpl(SessionFactoryImpl sessionFactory) {
this.sessionFactory = sessionFactory;
this.statementInspector = sessionFactory.getSessionFactoryOptions().getStatementInspector();
CurrentTenantIdentifierResolver<Object> tenantIdentifierResolver = sessionFactory.getCurrentTenantIdentifierResolver();
if ( tenantIdentifierResolver != null ) {
@ -1504,6 +1506,12 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im
return this;
}
@Override
public StatelessSessionBuilder statementInspector(StatementInspector statementInspector) {
this.statementInspector = statementInspector;
return this;
}
@Override
public boolean shouldAutoJoinTransactions() {
return true;
@ -1547,7 +1555,7 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im
@Override
public StatementInspector getStatementInspector() {
return null;
return statementInspector;
}
@Override

View File

@ -22,8 +22,10 @@ import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.StatelessSession;
import org.hibernate.StatelessSessionBuilder;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SessionFactory;
@ -42,7 +44,6 @@ import static org.hamcrest.MatcherAssert.assertThat;
@DomainModel(
annotatedClasses = StatelessDoWorkTest.TestEntity.class
)
@SessionFactory
public class StatelessDoWorkTest {
public static final String EXPECTED_ENTITY_NAME = "test";
public static final Integer PERSISTED_TEST_ENTITY_ID = 1;
@ -68,6 +69,7 @@ public class StatelessDoWorkTest {
}
@Test
@SessionFactory
public void testDoReturningWork(SessionFactoryScope scope) {
String retrievedEntityName;
try (StatelessSession statelessSession = scope.getSessionFactory().openStatelessSession()) {
@ -91,6 +93,7 @@ public class StatelessDoWorkTest {
}
@Test
@SessionFactory
public void testDoWork(SessionFactoryScope scope) {
try (StatelessSession statelessSession = scope.getSessionFactory().openStatelessSession()) {
statelessSession.doWork(
@ -106,6 +109,31 @@ public class StatelessDoWorkTest {
assertThatAllTestEntitiesHaveBeenDeleted( scope );
}
@Test
@SessionFactory(useCollectingStatementInspector = true)
public void testStatelessSessionWithStatementInspector(SessionFactoryScope scope) {
SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
statementInspector.clear();
scope.inStatelessSession(
session -> {
session.createQuery( "from TestEntity", TestEntity.class ).list();
statementInspector.assertExecutedCount( 1 );
}
);
}
@Test
@SessionFactory
public void testStatelessSessionWithStatementInspector2(SessionFactoryScope scope) {
SQLStatementInspector statementInspector = new SQLStatementInspector();
final StatelessSessionBuilder statelessSessionBuilder = scope.getSessionFactory().withStatelessOptions().statementInspector( statementInspector );
StatelessSession session = statelessSessionBuilder.openStatelessSession();
session.createQuery( "from TestEntity", TestEntity.class ).list();
statementInspector.assertExecutedCount( 1 );
statementInspector.clear();
session.close();
}
private void assertThatAllTestEntitiesHaveBeenDeleted(SessionFactoryScope scope) {
scope.inTransaction( session -> {
List results = session.createQuery( "from TestEntity" ).list();