Remove 'statementInspectorClass' from @Jpa and refactor its usage to use

a @SettingProvider

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2021-11-22 23:53:15 +01:00 committed by Jan Schatteman
parent cc43aaefb9
commit ce6850bdfa
10 changed files with 66 additions and 45 deletions

View File

@ -25,6 +25,7 @@ import org.hibernate.resource.jdbc.spi.StatementInspector;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.jdbc.SQLStatementInspector; import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.jdbc.DefaultSQLStatementInspectorSettingProvider;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa; import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect; import org.hibernate.testing.orm.junit.RequiresDialect;
@ -48,7 +49,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
settingProviders = { settingProviders = {
@SettingProvider( @SettingProvider(
settingName = AvailableSettings.STATEMENT_INSPECTOR, settingName = AvailableSettings.STATEMENT_INSPECTOR,
provider = NamedQueryCommentTest.StatementInspectorSettingProvider.class provider = DefaultSQLStatementInspectorSettingProvider.class
) )
} }
) )
@ -61,6 +62,9 @@ public class NamedQueryCommentTest {
@BeforeAll @BeforeAll
public void setUp(EntityManagerFactoryScope scope) { public void setUp(EntityManagerFactoryScope scope) {
statementInspector = scope.getStatementInspector( SQLStatementInspector.class );
scope.inTransaction( scope.inTransaction(
entityManager -> { entityManager -> {
for ( String title : GAME_TITLES ) { for ( String title : GAME_TITLES ) {
@ -292,11 +296,4 @@ public class NamedQueryCommentTest {
} }
} }
public static class StatementInspectorSettingProvider implements SettingProvider.Provider<StatementInspector> {
@Override
public StatementInspector getSetting() {
statementInspector = new SQLStatementInspector();
return statementInspector;
}
}
} }

View File

@ -8,15 +8,14 @@ package org.hibernate.orm.test.mapping.onetoone;
import jakarta.persistence.CascadeType; import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinColumn;
import jakarta.persistence.MapsId; import jakarta.persistence.MapsId;
import jakarta.persistence.OneToOne; import jakarta.persistence.OneToOne;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.orm.jdbc.DefaultSQLStatementInspectorSettingProvider;
import org.hibernate.testing.jdbc.SQLStatementInspector; import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa; import org.hibernate.testing.orm.junit.Jpa;
@ -37,19 +36,12 @@ import static org.junit.jupiter.api.Assertions.assertSame;
settingProviders = { settingProviders = {
@SettingProvider( @SettingProvider(
settingName = AvailableSettings.STATEMENT_INSPECTOR, settingName = AvailableSettings.STATEMENT_INSPECTOR,
provider = OneToOneMapsIdJoinColumnTest.SQLStatementInspectorProvider.class provider = DefaultSQLStatementInspectorSettingProvider.class
) )
} }
) )
public class OneToOneMapsIdJoinColumnTest { public class OneToOneMapsIdJoinColumnTest {
public static class SQLStatementInspectorProvider implements SettingProvider.Provider<Class> {
@Override
public Class getSetting() {
return SQLStatementInspector.class;
}
}
@BeforeEach @BeforeEach
public void setUp(EntityManagerFactoryScope scope) { public void setUp(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> { scope.inTransaction( entityManager -> {
@ -67,7 +59,7 @@ public class OneToOneMapsIdJoinColumnTest {
@Test @Test
public void testLifecycle(EntityManagerFactoryScope scope) { public void testLifecycle(EntityManagerFactoryScope scope) {
SQLStatementInspector statementInspector = getSqlStatementInspector( scope ); SQLStatementInspector statementInspector = scope.getStatementInspector( SQLStatementInspector.class );
statementInspector.clear(); statementInspector.clear();
scope.inTransaction( entityManager -> { scope.inTransaction( entityManager -> {
@ -85,12 +77,6 @@ public class OneToOneMapsIdJoinColumnTest {
} ); } );
} }
private SQLStatementInspector getSqlStatementInspector(EntityManagerFactoryScope scope) {
EntityManagerFactory entityManagerFactory = scope.getEntityManagerFactory();
SessionFactory sessionFactory = entityManagerFactory.unwrap( SessionFactory.class );
return (SQLStatementInspector) sessionFactory.getSessionFactoryOptions().getStatementInspector();
}
@Entity(name = "Person") @Entity(name = "Person")
public static class Person { public static class Person {

View File

@ -13,9 +13,11 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.jdbc.SQLStatementInspector; import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.jdbc.DefaultSQLStatementInspectorSettingProvider;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa; import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.Setting; import org.hibernate.testing.orm.junit.Setting;
import org.hibernate.testing.orm.junit.SettingProvider;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -30,7 +32,6 @@ import jakarta.persistence.criteria.Root;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@ -41,7 +42,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
@Setting(name = AvailableSettings.USE_SQL_COMMENTS, value = "true"), @Setting(name = AvailableSettings.USE_SQL_COMMENTS, value = "true"),
@Setting(name = AvailableSettings.IN_CLAUSE_PARAMETER_PADDING, value = "true"), @Setting(name = AvailableSettings.IN_CLAUSE_PARAMETER_PADDING, value = "true"),
}, },
statementInspectorClass = SQLStatementInspector.class settingProviders = {
@SettingProvider(
settingName = AvailableSettings.STATEMENT_INSPECTOR,
provider = DefaultSQLStatementInspectorSettingProvider.class
)
}
) )
public class InClauseParameterPaddingCriteriaTest { public class InClauseParameterPaddingCriteriaTest {
@ -56,7 +62,7 @@ public class InClauseParameterPaddingCriteriaTest {
@Test @Test
public void testInClauseParameterPadding(EntityManagerFactoryScope scope) { public void testInClauseParameterPadding(EntityManagerFactoryScope scope) {
final SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector(); final SQLStatementInspector statementInspector = scope.getStatementInspector( SQLStatementInspector.class );
statementInspector.clear(); statementInspector.clear();
scope.inTransaction( entityManager -> { scope.inTransaction( entityManager -> {
@ -80,7 +86,7 @@ public class InClauseParameterPaddingCriteriaTest {
@Test @Test
public void testInClauseParameterPaddingForExpressions(EntityManagerFactoryScope scope) { public void testInClauseParameterPaddingForExpressions(EntityManagerFactoryScope scope) {
final SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector(); final SQLStatementInspector statementInspector = scope.getStatementInspector( SQLStatementInspector.class );
statementInspector.clear(); statementInspector.clear();
scope.inTransaction( entityManager -> { scope.inTransaction( entityManager -> {

View File

@ -12,9 +12,11 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.jdbc.SQLStatementInspector; import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.jdbc.DefaultSQLStatementInspectorSettingProvider;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa; import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.Setting; import org.hibernate.testing.orm.junit.Setting;
import org.hibernate.testing.orm.junit.SettingProvider;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -34,8 +36,12 @@ import static org.junit.Assert.assertTrue;
@Setting(name = AvailableSettings.USE_SQL_COMMENTS, value = "true"), @Setting(name = AvailableSettings.USE_SQL_COMMENTS, value = "true"),
@Setting(name = AvailableSettings.IN_CLAUSE_PARAMETER_PADDING, value = "true") @Setting(name = AvailableSettings.IN_CLAUSE_PARAMETER_PADDING, value = "true")
}, },
statementInspectorClass = SQLStatementInspector.class settingProviders = {
@SettingProvider(
settingName = AvailableSettings.STATEMENT_INSPECTOR,
provider = DefaultSQLStatementInspectorSettingProvider.class
)
}
) )
public class InClauseParameterPaddingTest { public class InClauseParameterPaddingTest {
@ -70,7 +76,7 @@ public class InClauseParameterPaddingTest {
EntityManagerFactoryScope scope, EntityManagerFactoryScope scope,
String expectedInClause, String expectedInClause,
Integer... ids) { Integer... ids) {
final SQLStatementInspector sqlStatementInterceptor = (SQLStatementInspector) scope.getStatementInspector(); final SQLStatementInspector sqlStatementInterceptor = scope.getStatementInspector( SQLStatementInspector.class );
sqlStatementInterceptor.clear(); sqlStatementInterceptor.clear();
scope.inTransaction( entityManager -> { scope.inTransaction( entityManager -> {

View File

@ -14,6 +14,7 @@ import org.hibernate.dialect.H2Dialect;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.jdbc.SQLStatementInspector; import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.jdbc.DefaultSQLStatementInspectorSettingProvider;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa; import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect; import org.hibernate.testing.orm.junit.RequiresDialect;
@ -43,8 +44,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
settingName = AvailableSettings.DIALECT, settingName = AvailableSettings.DIALECT,
provider = MaxInExpressionParameterPaddingTest.DialectProvider.class provider = MaxInExpressionParameterPaddingTest.DialectProvider.class
), ),
}, @SettingProvider(
statementInspectorClass = SQLStatementInspector.class settingName = AvailableSettings.STATEMENT_INSPECTOR,
provider = DefaultSQLStatementInspectorSettingProvider.class
)
}
) )
public class MaxInExpressionParameterPaddingTest { public class MaxInExpressionParameterPaddingTest {
@ -72,7 +76,7 @@ public class MaxInExpressionParameterPaddingTest {
@Test @Test
public void testInClauseParameterPadding(EntityManagerFactoryScope scope) { public void testInClauseParameterPadding(EntityManagerFactoryScope scope) {
final SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector(); final SQLStatementInspector statementInspector = scope.getStatementInspector( SQLStatementInspector.class );
statementInspector.clear(); statementInspector.clear();
scope.inTransaction( entityManager -> scope.inTransaction( entityManager ->
@ -94,7 +98,7 @@ public class MaxInExpressionParameterPaddingTest {
@TestForIssue(jiraKey = "HHH-14109") @TestForIssue(jiraKey = "HHH-14109")
@Test @Test
public void testInClauseParameterPaddingToLimit(EntityManagerFactoryScope scope) { public void testInClauseParameterPaddingToLimit(EntityManagerFactoryScope scope) {
final SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector(); final SQLStatementInspector statementInspector = scope.getStatementInspector( SQLStatementInspector.class );
statementInspector.clear(); statementInspector.clear();
scope.inTransaction( scope.inTransaction(
@ -119,7 +123,7 @@ public class MaxInExpressionParameterPaddingTest {
@Test @Test
public void testInClauseParameterSplittingAfterLimit(EntityManagerFactoryScope scope) { public void testInClauseParameterSplittingAfterLimit(EntityManagerFactoryScope scope) {
final SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector(); final SQLStatementInspector statementInspector = scope.getStatementInspector( SQLStatementInspector.class );
statementInspector.clear(); statementInspector.clear();
scope.inTransaction( scope.inTransaction(
@ -145,7 +149,7 @@ public class MaxInExpressionParameterPaddingTest {
@Test @Test
public void testInClauseParameterSplittingAfterLimit2(EntityManagerFactoryScope scope) { public void testInClauseParameterSplittingAfterLimit2(EntityManagerFactoryScope scope) {
final SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector(); final SQLStatementInspector statementInspector = scope.getStatementInspector( SQLStatementInspector.class );
statementInspector.clear(); statementInspector.clear();
scope.inTransaction( scope.inTransaction(
@ -171,7 +175,7 @@ public class MaxInExpressionParameterPaddingTest {
@Test @Test
public void testInClauseParameterSplittingAfterLimit3(EntityManagerFactoryScope scope) { public void testInClauseParameterSplittingAfterLimit3(EntityManagerFactoryScope scope) {
final SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector(); final SQLStatementInspector statementInspector = scope.getStatementInspector( SQLStatementInspector.class );
statementInspector.clear(); statementInspector.clear();
scope.inTransaction( scope.inTransaction(

View File

@ -0,0 +1,19 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.testing.orm.jdbc;
import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.SettingProvider;
public class DefaultSQLStatementInspectorSettingProvider implements SettingProvider.Provider<SQLStatementInspector> {
@Override
public SQLStatementInspector getSetting() {
return new SQLStatementInspector();
}
}

View File

@ -49,6 +49,12 @@ abstract class AbstractEntityManagerFactoryScope implements EntityManagerFactory
.getStatementInspector(); .getStatementInspector();
} }
@Override
public <T extends StatementInspector> T getStatementInspector(Class<T> type) {
//noinspection unchecked
return (T) getStatementInspector();
}
@Override @Override
public void close() { public void close() {
if ( !active ) { if ( !active ) {

View File

@ -99,9 +99,6 @@ public class EntityManagerFactoryExtension
pui.getProperties().put( AvailableSettings.JPA_ID_GENERATOR_GLOBAL_SCOPE_COMPLIANCE, emfAnn.generatorScopeComplianceEnabled() ); pui.getProperties().put( AvailableSettings.JPA_ID_GENERATOR_GLOBAL_SCOPE_COMPLIANCE, emfAnn.generatorScopeComplianceEnabled() );
pui.getProperties().put( AvailableSettings.JPA_ORDER_BY_MAPPING_COMPLIANCE, emfAnn.orderByMappingComplianceEnabled() ); pui.getProperties().put( AvailableSettings.JPA_ORDER_BY_MAPPING_COMPLIANCE, emfAnn.orderByMappingComplianceEnabled() );
pui.getProperties().put( AvailableSettings.JPA_LOAD_BY_ID_COMPLIANCE, emfAnn.loadByIdComplianceEnabled() ); pui.getProperties().put( AvailableSettings.JPA_LOAD_BY_ID_COMPLIANCE, emfAnn.loadByIdComplianceEnabled() );
if ( !emfAnn.statementInspectorClass().equals( StatementInspector.class ) ) {
pui.getProperties().put( AvailableSettings.STATEMENT_INSPECTOR, emfAnn.statementInspectorClass() );
}
final Setting[] properties = emfAnn.properties(); final Setting[] properties = emfAnn.properties();
for ( int i = 0; i < properties.length; i++ ) { for ( int i = 0; i < properties.length; i++ ) {

View File

@ -19,7 +19,9 @@ import org.hibernate.resource.jdbc.spi.StatementInspector;
public interface EntityManagerFactoryScope { public interface EntityManagerFactoryScope {
EntityManagerFactory getEntityManagerFactory(); EntityManagerFactory getEntityManagerFactory();
void releaseEntityManagerFactory(); void releaseEntityManagerFactory();
StatementInspector getStatementInspector(); StatementInspector getStatementInspector();
<T extends StatementInspector> T getStatementInspector(Class<T> type);
void inEntityManager(Consumer<EntityManager> action); void inEntityManager(Consumer<EntityManager> action);
void inTransaction(Consumer<EntityManager> action); void inTransaction(Consumer<EntityManager> action);

View File

@ -108,8 +108,6 @@ public @interface Jpa {
*/ */
boolean loadByIdComplianceEnabled() default false; boolean loadByIdComplianceEnabled() default false;
Class<? extends StatementInspector> statementInspectorClass() default StatementInspector.class;
boolean excludeUnlistedClasses() default false; boolean excludeUnlistedClasses() default false;
StandardDomainModel[] standardModels() default {}; StandardDomainModel[] standardModels() default {};