From 841368175f68c7d44e4c74c09852809a66a83e8b Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 3 Oct 2019 16:01:31 +0100 Subject: [PATCH] HHH-13614 Allow the IntegratorProvider to be supplied via its FQN in the JPA persistence.xml --- .../EntityManagerFactoryBuilderImpl.java | 37 +++-- .../DtoIntegratorProvider.java | 43 ++++++ ...IntegrationProviderSettingByClassTest.java | 48 ++++++ ...ntegrationProviderSettingByObjectTest.java | 48 ++++++ ...ntegrationProviderSettingByStringTest.java | 48 ++++++ .../jpa/test/integrationprovider/Person.java | 49 ++++++ .../test/integrationprovider/PersonDto.java | 37 +++++ .../test/query/ConstructorResultDtoTest.java | 139 ------------------ 8 files changed, 294 insertions(+), 155 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/DtoIntegratorProvider.java create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/IntegrationProviderSettingByClassTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/IntegrationProviderSettingByObjectTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/IntegrationProviderSettingByStringTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/Person.java create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/PersonDto.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/query/ConstructorResultDtoTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java b/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java index f2a777db8c..a64bde63f5 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java @@ -368,18 +368,8 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil ClassLoaderService providedClassLoaderService) { final BootstrapServiceRegistryBuilder bsrBuilder = new BootstrapServiceRegistryBuilder(); - final IntegratorProvider integratorProvider = loadSettingInstance( - INTEGRATOR_PROVIDER, - integrationSettings.get( INTEGRATOR_PROVIDER ), - IntegratorProvider.class - ); + applyIntegrationProvider( integrationSettings, bsrBuilder ); - if ( integratorProvider != null ) { - for ( Integrator integrator : integratorProvider.getIntegrators() ) { - bsrBuilder.applyIntegrator( integrator ); - } - } - final StrategyRegistrationProviderList strategyRegistrationProviderList = (StrategyRegistrationProviderList) integrationSettings.get( STRATEGY_REGISTRATION_PROVIDERS ); if ( strategyRegistrationProviderList != null ) { @@ -443,6 +433,25 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil return bsrBuilder.build(); } + private void applyIntegrationProvider(Map integrationSettings, BootstrapServiceRegistryBuilder bsrBuilder) { + Object integrationSetting = integrationSettings.get( INTEGRATOR_PROVIDER ); + if ( integrationSetting == null ) { + return; + } + final IntegratorProvider integratorProvider = loadSettingInstance( + INTEGRATOR_PROVIDER, + integrationSetting, + IntegratorProvider.class + ); + + if ( integratorProvider != null ) { + for ( Integrator integrator : integratorProvider.getIntegrators() ) { + bsrBuilder.applyIntegrator( integrator ); + } + } + } + + @SuppressWarnings("unchecked") private MergedSettings mergeSettings( PersistenceUnitDescriptor persistenceUnit, Map integrationSettings, @@ -1375,10 +1384,6 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil } private T loadSettingInstance(String settingName, Object settingValue, Class clazz) { - if ( settingValue == null ) { - return null; - } - T instance = null; Class instanceClass = null; @@ -1416,7 +1421,7 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil } catch (InstantiationException | IllegalAccessException e) { throw new IllegalArgumentException( - "The MetadataBuilderContributor class [" + instanceClass + "] could not be instantiated!", + "The " + clazz.getSimpleName() +" class [" + instanceClass + "] could not be instantiated!", e ); } diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/DtoIntegratorProvider.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/DtoIntegratorProvider.java new file mode 100644 index 0000000000..b71ce808b7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/DtoIntegratorProvider.java @@ -0,0 +1,43 @@ +/* + * 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.jpa.test.integrationprovider; + +import java.util.Collections; +import java.util.List; + +import org.hibernate.boot.Metadata; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.integrator.spi.Integrator; +import org.hibernate.jpa.boot.spi.IntegratorProvider; +import org.hibernate.service.spi.SessionFactoryServiceRegistry; + +/** + * @author Andrea Boriero + */ +public class DtoIntegratorProvider implements IntegratorProvider { + @Override + public List getIntegrators() { + return Collections.singletonList( + new Integrator() { + @Override + public void integrate( + Metadata metadata, + SessionFactoryImplementor sessionFactory, + SessionFactoryServiceRegistry serviceRegistry) { + metadata.getImports().put( "PersonDto", PersonDto.class.getName() ); + } + + @Override + public void disintegrate( + SessionFactoryImplementor sessionFactory, + SessionFactoryServiceRegistry serviceRegistry) { + + } + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/IntegrationProviderSettingByClassTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/IntegrationProviderSettingByClassTest.java new file mode 100644 index 0000000000..53595d2c9c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/IntegrationProviderSettingByClassTest.java @@ -0,0 +1,48 @@ +/* + * 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 . + */ +package org.hibernate.jpa.test.integrationprovider; + +import java.util.List; +import java.util.Map; + +import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; + +/** + * @author Vlad Mihalcea + */ +@TestForIssue(jiraKey = "HHH-13614") +public class IntegrationProviderSettingByClassTest extends BaseEntityManagerFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + Person.class + }; + } + + @Test + public void test() { + doInJPA( this::entityManagerFactory, entityManager -> { + List dtos = entityManager.createQuery( + "select new PersonDto(id, name) " + + "from Person", PersonDto.class ) + .getResultList(); + } ); + } + + @Override + protected void addMappings(Map settings) { + settings.put( EntityManagerFactoryBuilderImpl.INTEGRATOR_PROVIDER, DtoIntegratorProvider.class ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/IntegrationProviderSettingByObjectTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/IntegrationProviderSettingByObjectTest.java new file mode 100644 index 0000000000..52274ea9c4 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/IntegrationProviderSettingByObjectTest.java @@ -0,0 +1,48 @@ +/* + * 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 . + */ +package org.hibernate.jpa.test.integrationprovider; + +import java.util.List; +import java.util.Map; + +import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; + +/** + * @author Vlad Mihalcea + */ +@TestForIssue(jiraKey = "HHH-13614") +public class IntegrationProviderSettingByObjectTest extends BaseEntityManagerFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + Person.class + }; + } + + @Test + public void test() { + doInJPA( this::entityManagerFactory, entityManager -> { + List dtos = entityManager.createQuery( + "select new PersonDto(id, name) " + + "from Person", PersonDto.class ) + .getResultList(); + } ); + } + + @Override + protected void addMappings(Map settings) { + settings.put( EntityManagerFactoryBuilderImpl.INTEGRATOR_PROVIDER, new DtoIntegratorProvider() ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/IntegrationProviderSettingByStringTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/IntegrationProviderSettingByStringTest.java new file mode 100644 index 0000000000..ec0406e171 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/IntegrationProviderSettingByStringTest.java @@ -0,0 +1,48 @@ +/* + * 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 . + */ +package org.hibernate.jpa.test.integrationprovider; + +import java.util.List; +import java.util.Map; + +import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; + +/** + * @author Vlad Mihalcea + */ +@TestForIssue(jiraKey = "HHH-13614") +public class IntegrationProviderSettingByStringTest extends BaseEntityManagerFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + Person.class + }; + } + + @Test + public void test() { + doInJPA( this::entityManagerFactory, entityManager -> { + List dtos = entityManager.createQuery( + "select new PersonDto(id, name) " + + "from Person", PersonDto.class ) + .getResultList(); + } ); + } + + @Override + protected void addMappings(Map settings) { + settings.put( EntityManagerFactoryBuilderImpl.INTEGRATOR_PROVIDER, DtoIntegratorProvider.class.getName() ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/Person.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/Person.java new file mode 100644 index 0000000000..af6929569d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/Person.java @@ -0,0 +1,49 @@ +/* + * 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.jpa.test.integrationprovider; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +/** + * @author Andrea Boriero + */ +@Entity(name = "Person") +public class Person { + + @Id + @GeneratedValue + private Long id; + + private String name; + + private int age; + + public Person() { + } + + public Person(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/PersonDto.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/PersonDto.java new file mode 100644 index 0000000000..e69cf22e11 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/integrationprovider/PersonDto.java @@ -0,0 +1,37 @@ +/* + * 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.jpa.test.integrationprovider; + +/** + * @author Andrea Boriero + */ +public class PersonDto { + private Long id; + + private String name; + + public PersonDto(Long id, String name) { + this.id = id; + this.name = name; + } + + public void setId(Long id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/query/ConstructorResultDtoTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/query/ConstructorResultDtoTest.java deleted file mode 100644 index 3d46188919..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/query/ConstructorResultDtoTest.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.jpa.test.query; - -import java.util.*; -import javax.persistence.*; - -import org.hibernate.FlushMode; -import org.hibernate.Session; -import org.hibernate.boot.Metadata; -import org.hibernate.dialect.Oracle8iDialect; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.integrator.spi.Integrator; -import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; -import org.hibernate.jpa.boot.spi.IntegratorProvider; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; - -import org.hibernate.service.spi.SessionFactoryServiceRegistry; -import org.hibernate.testing.SkipForDialect; -import org.junit.Test; - -import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * @author Vlad Mihalcea - */ -public class ConstructorResultDtoTest extends BaseEntityManagerFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{ - Person.class - }; - } - - @Test - public void test() { - doInJPA(this::entityManagerFactory, entityManager -> { - List dtos = entityManager.createQuery( - "select new PersonDto(id, name) " + - "from Person", PersonDto.class) - .getResultList(); - }); - } - - @Override - protected void addMappings(Map settings) { - settings.put(EntityManagerFactoryBuilderImpl.INTEGRATOR_PROVIDER, DtoIntegratorProvider.class.getName()); - } - - public static class DtoIntegratorProvider implements IntegratorProvider { - @Override - public List getIntegrators() { - return Collections.singletonList( - new Integrator() { - @Override - public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { - metadata.getImports().put("PersonDto", PersonDto.class.getName()); - } - - @Override - public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { - - } - } - ); - } - } - - @Entity(name = "Person") - public static class Person { - - @Id - @GeneratedValue - private Long id; - - private String name; - - private int age; - - public Person() { - } - - public Person(String name) { - this.name = name; - } - - public Long getId() { - return id; - } - - public String getName() { - return name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - } - - public static class PersonDto { - - private Long id; - - private String name; - - public PersonDto(Long id, String name) { - this.id = id; - this.name = name; - } - - public void setId(Long id) { - this.id = id; - } - - public void setName(String name) { - this.name = name; - } - - public Long getId() { - return id; - } - - public String getName() { - return name; - } - } -}