Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring mvc多实体数据库持久性_Java_Spring_Hibernate_Spring Mvc_Jpa - Fatal编程技术网

Java Spring mvc多实体数据库持久性

Java 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

我在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://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);