Search code examples
mongodbhibernateormhibernate-ogm

Getting hibernate persistence-unit classes from both ORM and OGM


I'm trying to use both of hibernate's orm and ogm in the same application. In my persistence.xml I have two persistence-unit:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="orm-mysql">
        ...
        <class>...</class>
        ...
        <class>...</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        ...
    </persistence-unit>
    <persistence-unit name="ogm-mongodb" transaction-type="JTA">
        ...
        <class>...</class>
        ...
        <class>...</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        ...
    </persistence-unit>
    ...
</persistence>

I want to use the hibernate orm native api with both orm and ogm, and so I need to add the annotated classes to two different MetadataSources.
Since I have the persistence units I thought that I can do this:

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("orm-mysql");
Set<ManagedType<?>> managedTypes = entityManagerFactory.getMetamodel().getManagedTypes();
for (ManagedType type : managedTypes) {
    ormSources.addAnnotatedClass(type.getJavaType());
}

entityManagerFactory = Persistence.createEntityManagerFactory("ogm-mongo");
managedTypes = entityManagerFactory.getMetamodel().getManagedTypes();
for (ManagedType type : managedTypes) {
    ogmSources.addAnnotatedClass(type.getJavaType());
}

The problem is that in the first iteration, of the orm classes, I get both the orm and ogm classes that are included in orm-mysql and ogm-mongo.
The second time around it's fine though, I get only the ogm-mongo classes.

Can't figure out what's wrong, any ideas?


Solution

  • In case anyone makes this stupid mistake as well, I had:

    <persistence-unit name="orm-mysql">
        ...
        <class>...</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.archive.autodetection" value="class"/>
            ...
        </properties>
    </persistence-unit>
    

    Removing the hibernate.archive.autodetection property solved the problem.