0

I'm having troubles with "working with multiple databases" in Spring MVC - hibernate JPA

I have two databases called user_db and portal_db. I need to work with them in different jpa units.

here is my persistance.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

    <persistence-unit name="user_unit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>Package.Entity1</class>

        <properties>
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost/user_db" />
            <property name="hibernate.connection.useUnicode" value="true" />
            <property name="hibernate.connection.characterEncoding" value="UTF-8" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.username" value="postgres" />
            <property name="hibernate.connection.password" value="" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />
            <!--<property name="hibernate.hbm2ddl.auto" value="create-drop" />-->
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>

    <persistence-unit name="portal_unit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>Package.Entity2</class>

        <properties>
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost/portal_db" />
            <property name="hibernate.connection.useUnicode" value="true" />
            <property name="hibernate.connection.characterEncoding" value="UTF-8" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.username" value="postgres" />
            <property name="hibernate.connection.password" value="" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />
            <!--<property name="hibernate.hbm2ddl.auto" value="create-drop"/>-->
            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

Problem is When I use create-drop and run my project, it creates both of my entites in both databases. Like table that should be only created by Package.Entity1 in user_unit, is also created in portal_unit.

And when I select,insert,update my entites, I set persistance unit in each of my individual DAO's.

for example, in Entity Dao's implementation I have:

EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);

where persistenceUnit could be user_unit or portal_unit depending on implementation.

What changes should I make so it wont create same table in both databases?

2 Answers 2

1

Add <exclude-unlisted-classes>true</exclude-unlisted-classes> in both of yours persistence units OR use JPA 2.x

Sign up to request clarification or add additional context in comments.

1 Comment

It worked. thanks :) I mark is as correct answer as soon as possible.
1

Add <exclude-unlisted-classes>true</exclude-unlisted-classes> in both your persistence units.

Please follow post below for detailed explanation:

Multiple persistance unit in persistence.xml creating tables in one another

1 Comment

Thanks for the details :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.