简体   繁体   English

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

[英]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. 我有一个应用程序,它使用一组位于2个不同数据库中的JPA实体。 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: 我有两种PU:

        <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? 有没有办法标记哪个实体由哪个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. -edit:请注意,在@Table上添加“schema”属性并不能解决问题,因为每个PU都会尝试在正确的模式中创建相同的实体,并且会因为表已经存在而出现错误。

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> . 您需要列出每个持久单元下的实体,并使用<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来自动生成表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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