Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

org.hibernate.DuplicateMappingException in packaged JAR #258

Closed
jwgmeligmeyling opened this issue Sep 29, 2016 · 15 comments
Closed

org.hibernate.DuplicateMappingException in packaged JAR #258

jwgmeligmeyling opened this issue Sep 29, 2016 · 15 comments

Comments

@jwgmeligmeyling
Copy link
Collaborator

If I package my application to a JAR, start up fails with a org.hibernate.DuplicateMappingException, even though it runs just fine from my IDE.

My entities and persistence.xml live in a domain module, that is included in my application's pom.xml file.

    <dependency>
      <groupId>com.pallasathenagroup</groupId>
      <artifactId>domain</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

blaze-persistence is then loaded as a transitive dependency from domain. I am using the configuration from the README.md:

        <dependency>
            <groupId>com.blazebit</groupId>
            <artifactId>blaze-persistence-core-api</artifactId>
            <version>${blaze-persistence.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.blazebit</groupId>
            <artifactId>blaze-persistence-core-impl</artifactId>
            <version>${blaze-persistence.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.blazebit</groupId>
            <artifactId>blaze-persistence-entity-view-api</artifactId>
            <version>${blaze-persistence.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.blazebit</groupId>
            <artifactId>blaze-persistence-entity-view-impl</artifactId>
            <version>${blaze-persistence.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.blazebit</groupId>
            <artifactId>blaze-persistence-integration-hibernate-5.2</artifactId>
            <version>${blaze-persistence.version}</version>
            <scope>runtime</scope>
        </dependency>

When I run the application from the JAR, using java -classpath target/core-full.jar com.pallasathenagroup.portal.core.Iris the application fails with:

Exception in thread "main" org.hibernate.DuplicateMappingException: Duplicate entity mapping com.blazebit.persistence.impl.function.entity.ValuesEntity
        at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.addEntityBinding(InFlightMetadataCollectorImpl.java:271)
        at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:760)
        at com.blazebit.persistence.impl.hibernate.Hibernate52MetadataContributor.addEntity(Hibernate52MetadataContributor.java:43)
        at com.blazebit.persistence.impl.hibernate.Hibernate52MetadataContributor.contribute(Hibernate52MetadataContributor.java:35)
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:275)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:857)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:884)
        at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
        at com.pallasathenagroup.portal.core.IrisModule.provideEntityManagerFactory(IrisModule.java:84)
        at com.pallasathenagroup.portal.core.IrisModule_ProvideEntityManagerFactoryFactory.get(IrisModule_ProvideEntityManagerFactoryFactory.java:22)
        at com.pallasathenagroup.portal.core.IrisModule_ProvideEntityManagerFactoryFactory.get(IrisModule_ProvideEntityManagerFactoryFactory.java:8)
        at dagger.internal.ScopedProvider.get(ScopedProvider.java:46)
        at com.pallasathenagroup.portal.core.DaggerIris_IrisGraph.getEntityManagerFactory(DaggerIris_IrisGraph.java:272)
        at com.pallasathenagroup.portal.core.Iris.go(Iris.java:85)
        at com.pallasathenagroup.portal.core.Iris.main(Iris.java:142)

Any idea what could cause this issue?

@beikov
Copy link
Member

beikov commented Sep 29, 2016

I guess that annotation scanning picks up the ValuesEntity class from blaze-persistence-core-impl.jar but the integration tries to also add it. You could workaround that for now by declaring the classes explicitly and disable scanning by setting exclude-unlisted-classes to true in your persistence.xml
I will adapt the integration so that it doesn't get added twice.

@beikov beikov self-assigned this Sep 29, 2016
@beikov beikov added this to the 1.2.0 milestone Sep 29, 2016
@jwgmeligmeyling
Copy link
Collaborator Author

That means I have to add all of my @Entitys , including the @CTEs, and without this ValuesEntity right?

@beikov
Copy link
Member

beikov commented Sep 29, 2016

Yes unfortunately.

@jwgmeligmeyling
Copy link
Collaborator Author

I changed my persistence.xml to include the class files and exclude unlisted classes. However, now I get the following exception:

Exception in thread "main" java.util.ServiceConfigurationError: com.blazebit.persistence.spi.EntityManagerFactoryIntegrator: Provider com.blazebit.persistence.impl.hibernate.function.HibernateEntityManagerFactoryIntegrator could not be instantiated
        at java.util.ServiceLoader.fail(ServiceLoader.java:232)
        at java.util.ServiceLoader.access$100(ServiceLoader.java:185)
        at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:384)
        at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
        at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
        at com.blazebit.persistence.impl.CriteriaBuilderConfigurationImpl.loadEntityManagerIntegrator(CriteriaBuilderConfigurationImpl.java:437)
        at com.blazebit.persistence.impl.CriteriaBuilderConfigurationImpl.<init>(CriteriaBuilderConfigurationImpl.java:147)
        at com.blazebit.persistence.impl.CriteriaBuilderConfigurationProviderImpl.createConfiguration(CriteriaBuilderConfigurationProviderImpl.java:40)
        at com.blazebit.persistence.Criteria.getDefault(Criteria.java:54)
        at com.pallasathenagroup.portal.core.IrisModule.provideCriteriaBuilderFactory(IrisModule.java:57)
        at com.pallasathenagroup.portal.core.IrisModule_ProvideCriteriaBuilderFactoryFactory.get(IrisModule_ProvideCriteriaBuilderFactoryFactory.java:23)
        at com.pallasathenagroup.portal.core.IrisModule_ProvideCriteriaBuilderFactoryFactory.get(IrisModule_ProvideCriteriaBuilderFactoryFactory.java:9)
        at dagger.internal.ScopedProvider.get(ScopedProvider.java:46)
        at com.pallasathenagroup.portal.core.DaggerIris_IrisGraph.getCriteriaBuilderFactory(DaggerIris_IrisGraph.java:277)
        at com.pallasathenagroup.portal.core.Iris.go(Iris.java:86)
        at com.pallasathenagroup.portal.core.Iris.main(Iris.java:142)
Caused by: java.lang.ExceptionInInitializerError
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
        at java.lang.Class.newInstance(Class.java:442)
        at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:380)
        ... 13 more
Caused by: java.lang.NumberFormatException: For input string: "[WORKING]"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:580)
        at java.lang.Integer.parseInt(Integer.java:615)
        at com.blazebit.persistence.impl.hibernate.function.HibernateEntityManagerFactoryIntegrator.<clinit>(HibernateEntityManagerFactoryIntegrator.java:75)
        ... 19 more

@beikov
Copy link
Member

beikov commented Sep 29, 2016

What kind of custom Hibernate version are you using? ^^
I am expecting a dot separated version string.

@jwgmeligmeyling
Copy link
Collaborator Author

mvn dependency:tree lists:

[INFO] +- com.pallasathenagroup:domain:jar:1.0-SNAPSHOT:compile
[INFO] |  +- org.hibernate:hibernate-core:jar:5.2.1.Final:compile

Unless my ~/.m2 is corrupt, this should be the regular 5.2.1.Final release.

@jwgmeligmeyling
Copy link
Collaborator Author

[WORKING] actually seems to be the fallback if the implementation version of the JAR can not be found, which probably is the case because I am using a fat jar.

@beikov
Copy link
Member

beikov commented Sep 29, 2016

Apparently the version is read from the manifest which is removed when using fat jar.

@beikov
Copy link
Member

beikov commented Sep 29, 2016

Any idea how I could infer the version in the fat jar case?

@jwgmeligmeyling
Copy link
Collaborator Author

jwgmeligmeyling commented Sep 29, 2016

Currently trying to research that, maybe I can somehow include it in my own MANIFEST.MF. Playing with the manifest configuration for maven-assembly-plugin now.

@jwgmeligmeyling
Copy link
Collaborator Author

After a bit of research I have come to the conclusion that this is impossible. If Version is really part of Hibernate's external API, they probably should use other methods to provide the version number, for example by providing the ${project.version} using resource filtering. I will now work around the issue by simply not using a fat jar.

That still leaves the issue DuplicateMappingException described in this issue. The work around indeed works for me!

@beikov
Copy link
Member

beikov commented Sep 30, 2016

Apparently this changed recently: hibernate/hibernate-orm@015be3d#diff-197d2841569f03ef77fff9b387e93425

@beikov
Copy link
Member

beikov commented Sep 30, 2016

Maybe I can get them to fix that for 5.2.3

@beikov
Copy link
Member

beikov commented Sep 30, 2016

Too bad, they are just about to release 5.2.3 so fat-jar support will have to wait for at least 5.2.4.
You could workaround it I guess by customizing the manifest

@beikov
Copy link
Member

beikov commented Nov 8, 2016

Just as a side note, it doesn't look like the Hibernate team is going to fix this anytime soon. As a workaround you could try to give implementing #274 a shot which would be a good workaround I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants