From e8494965b9235947e454a127785c9117a7eb2014 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Thu, 29 Sep 2016 19:28:46 +0200 Subject: [PATCH] Fixed duplicate entity #258 --- .../impl/hibernate/Hibernate4Integrator.java | 14 ++++++++++++-- .../impl/hibernate/Hibernate43Integrator.java | 11 +++++++++-- .../hibernate/Hibernate52MetadataContributor.java | 5 +++++ .../hibernate/Hibernate5MetadataContributor.java | 5 +++++ .../hibernate/Hibernate60MetadataContributor.java | 5 +++++ 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/integration/hibernate-4.2/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate4Integrator.java b/integration/hibernate-4.2/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate4Integrator.java index c19fa37246..98c9995bd5 100644 --- a/integration/hibernate-4.2/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate4Integrator.java +++ b/integration/hibernate-4.2/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate4Integrator.java @@ -41,9 +41,10 @@ public Class getServiceInitiated() { @Override public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { + Class valuesEntity; + boolean registerValuesEntity = true; try { - configuration.addAnnotatedClass(Class.forName("com.blazebit.persistence.impl.function.entity.ValuesEntity")); - configuration.buildMappings(); + valuesEntity = Class.forName("com.blazebit.persistence.impl.function.entity.ValuesEntity"); } catch (ClassNotFoundException e) { throw new RuntimeException("Are you missing blaze-persistence-core-impl on the classpath?", e); } @@ -52,6 +53,9 @@ public void integrate(Configuration configuration, SessionFactoryImplementor ses while (iter.hasNext()) { PersistentClass clazz = iter.next(); Class entityClass = clazz.getMappedClass(); + if (valuesEntity.equals(entityClass)) { + registerValuesEntity = false; + } if (entityClass.isAnnotationPresent(CTE.class)) { clazz.getTable().setSubselect("select * from " + clazz.getJpaEntityName()); @@ -59,6 +63,12 @@ public void integrate(Configuration configuration, SessionFactoryImplementor ses } } + if (registerValuesEntity) { + // Register values entity if wasn't found + configuration.addAnnotatedClass(valuesEntity); + configuration.buildMappings(); + } + serviceRegistry.locateServiceBinding(PersisterClassResolver.class).setService(new CustomPersisterClassResolver()); serviceRegistry.locateServiceBinding(Database.class).setService(new SimpleDatabase(configuration.getTableMappings())); } diff --git a/integration/hibernate-4.3/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate43Integrator.java b/integration/hibernate-4.3/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate43Integrator.java index 68716e340c..f27240a38b 100644 --- a/integration/hibernate-4.3/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate43Integrator.java +++ b/integration/hibernate-4.3/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate43Integrator.java @@ -41,9 +41,10 @@ public Class getServiceInitiated() { @Override public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { + Class valuesEntity; + boolean registerValuesEntity = true; try { - configuration.addAnnotatedClass(Class.forName("com.blazebit.persistence.impl.function.entity.ValuesEntity")); - configuration.buildMappings(); + valuesEntity = Class.forName("com.blazebit.persistence.impl.function.entity.ValuesEntity"); } catch (ClassNotFoundException e) { throw new RuntimeException("Are you missing blaze-persistence-core-impl on the classpath?", e); } @@ -59,6 +60,12 @@ public void integrate(Configuration configuration, SessionFactoryImplementor ses } } + if (registerValuesEntity) { + // Register values entity if wasn't found + configuration.addAnnotatedClass(valuesEntity); + configuration.buildMappings(); + } + serviceRegistry.locateServiceBinding(PersisterClassResolver.class).setService(new CustomPersisterClassResolver()); serviceRegistry.locateServiceBinding(Database.class).setService(new SimpleDatabase(configuration.getTableMappings())); } diff --git a/integration/hibernate-5.2/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate52MetadataContributor.java b/integration/hibernate-5.2/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate52MetadataContributor.java index f79bab06f6..853ecb675f 100644 --- a/integration/hibernate-5.2/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate52MetadataContributor.java +++ b/integration/hibernate-5.2/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate52MetadataContributor.java @@ -19,6 +19,11 @@ public class Hibernate52MetadataContributor implements MetadataContributor { @Override public void contribute(InFlightMetadataCollector metadataCollector, IndexView jandexIndex) { + // Skip if already registered + if (metadataCollector.getEntityBinding("com.blazebit.persistence.impl.function.entity.ValuesEntity") != null) { + return; + } + MetadataBuildingOptions options = metadataCollector.getMetadataBuildingOptions(); final ClassLoaderService classLoaderService = options.getServiceRegistry().getService( ClassLoaderService.class ); diff --git a/integration/hibernate-5/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate5MetadataContributor.java b/integration/hibernate-5/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate5MetadataContributor.java index a6fda43c64..25d04a6d95 100644 --- a/integration/hibernate-5/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate5MetadataContributor.java +++ b/integration/hibernate-5/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate5MetadataContributor.java @@ -16,6 +16,11 @@ public class Hibernate5MetadataContributor implements MetadataContributor { @Override public void contribute(InFlightMetadataCollector metadataCollector, IndexView jandexIndex) { + // Skip if already registered + if (metadataCollector.getEntityBinding("com.blazebit.persistence.impl.function.entity.ValuesEntity") != null) { + return; + } + MetadataBuildingOptions options = metadataCollector.getMetadataBuildingOptions(); final ClassLoaderService classLoaderService = options.getServiceRegistry().getService( ClassLoaderService.class ); diff --git a/integration/hibernate-6.0/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate60MetadataContributor.java b/integration/hibernate-6.0/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate60MetadataContributor.java index 9cd0270f44..92f386ae56 100644 --- a/integration/hibernate-6.0/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate60MetadataContributor.java +++ b/integration/hibernate-6.0/src/main/java/com/blazebit/persistence/impl/hibernate/Hibernate60MetadataContributor.java @@ -19,6 +19,11 @@ public class Hibernate60MetadataContributor implements MetadataContributor { @Override public void contribute(InFlightMetadataCollector metadataCollector, IndexView jandexIndex) { + // Skip if already registered + if (metadataCollector.getEntityBinding("com.blazebit.persistence.impl.function.entity.ValuesEntity") != null) { + return; + } + MetadataBuildingOptions options = metadataCollector.getMetadataBuildingOptions(); final ClassLoaderService classLoaderService = options.getServiceRegistry().getService( ClassLoaderService.class );