JPA / Hibernate:具有多个持久性单元的模式生成 [英] JPA / Hibernate : schema generation with multiple persistence units

查看:194
本文介绍了JPA / Hibernate:具有多个持久性单元的模式生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序使用位于2个不同数据库中的一组JPA实体。我为它配置了多个持久化单元。



问题是我想使用模式生成自动生成模式,并且在两个数据库中都创建了所有实体。



< 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/>

而且,我想使用元数据自动获取实体。我不想提供手动脚本,因为我需要保持它与实体的最新状态。



有没有办法标记要生成的实体PU?



-edit:请注意,在@Table上添加模式属性并不能解决问题,因为每个PU都会尝试创建相同的实体在正确的模式中,并且会有错误,因为这些表已经存在。

解决方案

是的,您可以做到这一点。您需要列出每个持久单元下的实体,并使用< exclude-unlisted-classes> true< / exclude-unlisted-classes>

 <! -  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.

这篇关于JPA / Hibernate:具有多个持久性单元的模式生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆