cripox
cripox

Reputation: 542

JPA / Hibernate : schema generation with multiple persistence units

I have an application that uses a set of JPA entities that are located in 2 different databases. I configured it with multiple persistence units.

The problem is that I want to auto-generate the schema using schema-generation, and all the entities are created in both databases.

I have in both PUs:

        <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
        <property name="javax.persistence.schema-generation.create-source" value="metadata"/>
        <property name="javax.persistence.schema-generation.drop-source" value="metadata"/>

And, yes, I want to use the metadata to get the entities automatically. I do not want to provide a manual script, because I would need to keep it up to date with the entities.

Is there a way to mark which entity to be generated by which PU?

-edit: please be aware that adding "schema" property on @Table does not resolve the problem, because each PU will try to create the same entity in the right schema, and there will be errors because the tables will already exist.

Upvotes: 1

Views: 1587

Answers (2)

K139
K139

Reputation: 3669

Yes, you can do that. You need to list the entities under each persistant unit, and also DISABLE the auto discovery of the unlisted entities explicitly with <exclude-unlisted-classes>true</exclude-unlisted-classes>.

  <!--  Unit 1 -->
  <persistence-unit name="Unit1" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.your.class.A</class>

    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.username" value=""/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
      <property name="javax.persistence.schema-generation.create-source" value="metadata"/>
      <property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
    </properties>
  </persistence-unit>

 <!--  Unit 2 -->
  <persistence-unit name="Unit2" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.your.class.B</class>

    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.username" value=""/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
      <property name="javax.persistence.schema-generation.create-source" value="metadata"/>
      <property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
    </properties>
  </persistence-unit>

Edit

If you are using annotations configuration, then

LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setPackagesToScan("com.A");

And another factory for another entity manager with a different package name.

Upvotes: 3

cripox
cripox

Reputation: 542

I found a way: I will use "schema" property in @Table annotation for each entity and then I would enable only one PU to auto generate the tables.

Upvotes: 0

Related Questions