Spring MVC多实体数据库持久性 [英] Spring mvc multiple entities database persistance
问题描述
Spring MVC中的使用多个数据库"问题-休眠JPA
I'm having troubles with "working with multiple databases" in Spring MVC - hibernate JPA
我有两个名为 user_db 和 portal_db 的数据库.我需要在不同的jpa单元中与他们合作.
I have two databases called user_db and portal_db. I need to work with them in different jpa units.
这是我的 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>
问题是当我使用create-drop并运行我的项目时,它在两个数据库中都创建了两个实体.像应该仅由user_unit中的Package.Entity1
创建的表一样,也是在portal_unit中创建的表.
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.
当我选择,插入,更新我的实体时,我在每个单独的DAO中都设置了持久性单元.
And when I select,insert,update my entites, I set persistance unit in each of my individual DAO's.
例如,在Entity Dao的实现中,我有:
for example, in Entity Dao's implementation I have:
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
其中的 persistenceUnit 可以是user_unit
或portal_unit
,具体取决于实现方式.
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?
推荐答案
在两个持久性单元中都添加<exclude-unlisted-classes>true</exclude-unlisted-classes>
或使用JPA 2.x
Add <exclude-unlisted-classes>true</exclude-unlisted-classes>
in both of yours persistence units OR use JPA 2.x
这篇关于Spring MVC多实体数据库持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!