添加多个entitymanager时出错

时间:2016-04-25 13:58:16

标签: java spring hibernate jpa

我为本地数据库创建了两个数据源,另一个用于从不同数据库中读取一些数据。但是,在启动服务器时,我得到以下异常:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:annotation-config />
<context:component-scan base-package="com.nm" />

<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter"/>
        </list>
    </property>
</bean>

<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

</bean>


<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
    <property name="driverClassName" value="${dbdriverClassName}" />
    <property name="url" value="${db-url}" />
    <property name="username" value="${db-username}" />
    <property name="password" value="${db-password}" />
    <!-- <property name="initialPoolSize" value="1" /> <property name="minPoolSize" 
        value="1" /> <property name="maxPoolSize" value="10" /> -->
</bean>


<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource2">
    <property name="driverClassName" value="${dbdriverClassName2}" />
    <property name="url" value="${db-url2}" />
    <property name="username" value="${db-username2}" />
    <property name="password" value="${db-password2}" />
    <!-- <property name="initialPoolSize" value="1" /> <property name="minPoolSize"
        value="1" /> <property name="maxPoolSize" value="10" /> -->
</bean>

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit" />
    <property name="dataSource" ref="dataSource" />
</bean>

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory2">
    <property name="persistenceUnitName" value="persistenceUnit2" />
    <property name="dataSource" ref="dataSource2" />
</bean>


<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager2">
    <property name="entityManagerFactory" ref="entityManagerFactory2" />
</bean>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:property-files/config-info.properties" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>
<tx:annotation-driven transaction-manager="transactionManager2"/>
</beans>

下面是我的春天xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit"
    transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.connection.charSet" value="UTF-8" />
        <property name="hibernate.search.default.directory_provider" value="filesystem" />
        <property name="hibernate.search.default.indexBase" value="/home/pallav/community-api/indexes" />
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.EJB3NamingStrategy" />
    </properties>
</persistence-unit>
<persistence-unit name="persistenceUnit2"
                  transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.hbm2ddl.auto" value="validate" />
        <property name="hibernate.connection.charSet" value="UTF-8" />
        <property name="hibernate.search.default.directory_provider" value="filesystem" />
        <property name="hibernate.search.default.indexBase" value="/home/pallav/community-api/indexes" />
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.EJB3NamingStrategy" />
    </properties>
</persistence-unit>

的persistence.xml

@PersistenceUnit(unitName="persistenceUnit")
@Entity
public class Answer implements BaseEntity{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@ManyToOne
private Question question;

private String description;

private int likes;

private int spam;

private int comments;

private short status;
- - - 

以下是实体

Answer

我不希望git status表位于第二个数据库中。 请帮助我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

以下是我根据持久性单元隔离实体所做的工作。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit"
    transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.nm.community.entities.Question</class>
    <class>com.nm.community.entities.Answer</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.connection.charSet" value="UTF-8" />
        <property name="hibernate.search.default.directory_provider" value="filesystem" />
        <property name="hibernate.search.default.indexBase" value="/home/pallav/community-api/indexes" />
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.EJB3NamingStrategy" />
        <!-- <property name="hibernate.search.default.indexBase" value="../community-api/indexes" /> -->
    </properties>
</persistence-unit>
<persistence-unit name="persistenceUnit2"
                  transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.nm.community.entities.MasterProductEntity</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.hbm2ddl.auto" value="validate" />
        <property name="hibernate.connection.charSet" value="UTF-8" />
        <property name="hibernate.search.default.directory_provider" value="filesystem" />
        <property name="hibernate.search.default.indexBase" value="/home/pallav/community-api/indexes" />
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.EJB3NamingStrategy" />
        <!-- <property name="hibernate.search.default.indexBase" value="../community-api/indexes" /> -->
    </properties>
</persistence-unit>

每个持久性单元都提供了datasource在服务器启动时所查找的类的名称。此外,<exclude-unlisted-classes>应为true