Hibernate / JPA:将实体映射到不同的数据库

时间:2022-05-25 20:01:28

I have an application which manages 3 databases. I use hibernate with JPA on seam framework.

我有一个管理3个数据库的应用程序。我在seam框架上使用了带有JPA的hibernate。

So I have a persitence.xml file with three persitence-unit like this (I remove properties for db2 and db3):

所以我有一个persitence.xml文件,有三个像这样的persitence-unit(我删除了db2和db3的属性):

<persistence-unit name="db1" transaction-type="JTA" >
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>db1source</jta-data-source>
    <properties>
        <property name="hibernate.dialect"
            value="org.hibernate.dialect.Oracle10gDialect" />
        <property name="hibernate.connection.driver_class"
            value="oracle.jdbc.driver.OracleDriver" />
        <property name="hibernate.hbm2ddl.auto" value="validate" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.default_schema" value="SI_TEC" />
        <property name="hibernate.validator.apply_to_ddl" value="false" />
        <property name="hibernate.transaction.manager_lookup_class"
            value="org.hibernate.transaction.WeblogicTransactionManagerLookup" />
    </properties>
</persistence-unit>

<persistence-unit name="db2" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>d2source</jta-data-source>
</persistence-unit>

<persistence-unit name="db3" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>d3source</jta-data-source>
</persistence-unit>

In my seam components.xml file, I create 3 managed-persistence-context to map seam with my hibernate configuration.

在我的seam components.xml文件中,我创建了3个托管持久化上下文来使用我的hibernate配置映射seam。

Finally, I have several entities and my problem is here. I need to persist some entities in db2 and other in db3. So database schema are different and when I deploy my application, I get this error:

最后,我有几个实体,我的问题就在这里。我需要在db2中保留一些实体,在db3中保留其他实体。所以数据库模式是不同的,当我部署我的应用程序时,我收到此错误:

org.hibernate.HibernateException: Missing table: PORTAILPERMISSION
    at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1113)
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:139)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:349)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    Truncated. see log file for complete stacktrace

Because, the table PORTAILPERMISSION doesn't exist in db2.

因为,db2中不存在表PORTAILPERMISSION。

My question is:

我的问题是:

How to specify in entity class what database (or persitence-unit) must be used to validate entity in startup ?

如何在实体类中指定在启动时必须使用哪个数据库(或persitence-unit)来验证实体?

Thanks for your help.

谢谢你的帮助。

1 个解决方案

#1


4  

You try to explicitly list classes (<class>..</class>) in each persistence unit. And use

您尝试在每个持久性单元中显式列出类( .. )。并使用

<exclude-unlisted-classes>true</exclude-unlisted-classes>

#1


4  

You try to explicitly list classes (<class>..</class>) in each persistence unit. And use

您尝试在每个持久性单元中显式列出类( .. )。并使用

<exclude-unlisted-classes>true</exclude-unlisted-classes>