简体   繁体   中英

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.

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.

我找到了一种方法:我将在每个实体的@Table注释中使用“schema”属性,然后我只启用一个PU来自动生成表。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM