2016-04-25 175 views
0

我創建了兩個數據源,一個用於本地數據庫,另一個用於從不同數據庫讀取一些數據。但是在啓動服務器了,我得到以下異常:添加多個實體管理器時出錯

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory2' defined in file [/home/pallav/91mobiles/community-api/dist/WEB-INF/classes/spring-config/spring-context.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Missing table: Answer 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956) ~[spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747) ~[spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) ~[spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) [spring-web-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) [spring-web-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) [spring-web-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4811) [catalina.jar:8.0.33] 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5251) [catalina.jar:8.0.33] 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) [catalina.jar:8.0.33] 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) [catalina.jar:8.0.33] 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701) [catalina.jar:8.0.33] 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717) [catalina.jar:8.0.33] 
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:587) [catalina.jar:8.0.33] 
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1798) [catalina.jar:8.0.33] 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_77] 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_77] 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_77] 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_77] 
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_77] 
Caused by: org.hibernate.HibernateException: Missing table: Answer 
    at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1335) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] 
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:175) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] 
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:525) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) ~[hibernate-entitymanager-4.3.3.Final.jar:4.3.3.Final] 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843) ~[hibernate-entitymanager-4.3.3.Final.jar:4.3.3.Final] 
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842) ~[hibernate-entitymanager-4.3.3.Final.jar:4.3.3.Final] 
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:150) ~[hibernate-entitymanager-4.3.3.Final.jar:4.3.3.Final] 
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) ~[spring-orm-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE] 
    ... 25 common frames omitted 

下面是我的春天的xml:

<?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> 

的persistence.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> 

及以下實體

@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表在第二個數據庫中出現。 請幫助我。提前致謝。

回答

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