且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JPA / Hibernate:具有多个持久性单元的模式生成

更新时间:2023-01-15 09:03:32

是的,您可以做到这一点。您需要列出每个持久单元下的实体,并使用< exclude-unlisted-classes> true< / exclude-unlisted-classes> $ c显式禁用未列出实体的自动发现

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

< exclude-unlisted-classes> true< / exclude-unlisted-classes>
<属性>
< property name =javax.persistence.jdbc.usernamevalue =/>
< property name =javax.persistence.jdbc.passwordvalue =/>
< property name =hibernate.hbm2ddl.autovalue =update/>
< property name =javax.persistence.schema-generation.database.actionvalue =drop-and-create/>
< property name =javax.persistence.schema-generation.create-sourcevalue =metadata/>
< property name =javax.persistence.schema-generation.drop-sourcevalue =metadata/>
< / properties>
< / persistence-unit>

<! - 第2单元 - >
< persistence-unit name =Unit2transaction-type =RESOURCE_LOCAL>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< class> com.your.class.B< / class>

< exclude-unlisted-classes> true< / exclude-unlisted-classes>
<属性>
< property name =javax.persistence.jdbc.usernamevalue =/>
< property name =javax.persistence.jdbc.passwordvalue =/>
< property name =hibernate.hbm2ddl.autovalue =update/>
< property name =javax.persistence.schema-generation.database.actionvalue =drop-and-create/>
< property name =javax.persistence.schema-generation.create-sourcevalue =metadata/>
< property name =javax.persistence.schema-generation.drop-sourcevalue =metadata/>
< / properties>
< / persistence-unit>



编辑



如果您使用注释配置,然后

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

另一个工厂用于另一个包名不同的实体经理。


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.