Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate/JPA: Mapping entities to different databases

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

So I have a persitence.xml file with three persitence-unit like this (I remove properties for db2 and 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.

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:

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.

My question is:

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

Thanks for your help.

like image 414
Kiva Avatar asked Aug 18 '10 10:08

Kiva


People also ask

How do you map an entity to multiple tables in Hibernate?

Yes, you can map an entity to 2 database tables in 2 simple steps: You need to annotate your entity with JPA's @Table and @SecondaryTable annotations and provide the names of the first and second table as the value of the name parameters.

How would you map two JPA or Hibernate entities on the same database table?

If you want to map the same database table to two entities, you should create a simple inheritance hierarchy. The superclass should be abstract and contain all attributes that are shared by both entities. You should map it as a mapped superclass so that it is not an entity itself.

What ways are available to you to map an object to database entities in Hibernate?

There are two ways: (1) using annotations in Java code, and (2) using XML files using either JPA standard format or Hibernate hbm.

Can a JPA entity have multiple OneToMany associations?

You can have multiple one-to-many associations, as long as only one is EAGER.


1 Answers

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

<exclude-unlisted-classes>true</exclude-unlisted-classes>
like image 178
Bozho Avatar answered Oct 15 '22 07:10

Bozho