Java Spring mvc多实体数据库持久性
我在SpringMVC-HibernateJPA中遇到了“使用多个数据库”的问题 我有两个数据库,分别是user_db和portal_db。我需要在不同的jpa单元中与他们合作 这是我的persistence.xmlJava Spring mvc多实体数据库持久性,java,spring,hibernate,spring-mvc,jpa,Java,Spring,Hibernate,Spring Mvc,Jpa,我在SpringMVC-HibernateJPA中遇到了“使用多个数据库”的问题 我有两个数据库,分别是user_db和portal_db。我需要在不同的jpa单元中与他们合作 这是我的persistence.xml <?xml version="1.0" encoding="UTF-8" ?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://w
<?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>
其中,persistenceUnit可以是user\u单元
或portal\u单元
,具体取决于实现
我应该做哪些更改,以使它不会在两个数据库中创建相同的表?在两个持久化单元中添加true
,或者在两个持久化单元中使用JPA2.x添加true
有关详细说明,请参见下面的帖子:
在两个持久化单元中添加true
有关详细说明,请参见下面的帖子:
成功了。谢谢:)我认为这是尽可能正确的答案。它起作用了。谢谢:)我的分数是尽可能正确的答案。谢谢你提供的详细信息:)谢谢你提供的详细信息:)
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);