javax.persistence.PersistenceException:无法构建EntityManagerFactory Tomcat 7 Spring Hibernate MS SQL

时间:2015-05-22 15:40:29

标签: java spring hibernate tomcat persistence

我有一个在Tomcat 7 w / MS SQL Server 2008上运行Hibernate 4.2.19的Spring 3.1.2 Web应用程序,我遇到了这个错误:

14:39:22.096 [localhost-startStop-1] DEBUG o.a.b.j.r.DefaultTraversableResolver - Found javax.persistence.PersistenceUtil on classpath.
14:39:22.098 [localhost-startStop-1] DEBUG o.a.b.j.r.DefaultTraversableResolver - Instantiated an instance of org.apache.bval.jsr303.resolver.JPATraversableResolver.
14:39:22.104 [localhost-startStop-1] DEBUG o.a.b.j.DefaultMessageInterpolator - ValidationMessages not found. Delegating to org.apache.bval.jsr303.ValidationMessages
14:39:22.113 [localhost-startStop-1] DEBUG o.a.bval.jsr303.xml.ValidationParser - No META-INF/validation.xml found. Using annotation based configuration only.
14:39:22.154 [localhost-startStop-1] DEBUG o.a.b.j.r.DefaultTraversableResolver - Found javax.persistence.PersistenceUtil on classpath.
14:39:22.155 [localhost-startStop-1] DEBUG o.a.b.j.r.DefaultTraversableResolver - Instantiated an instance of org.apache.bval.jsr303.resolver.JPATraversableResolver.
14:39:22.155 [localhost-startStop-1] DEBUG o.a.b.j.DefaultMessageInterpolator - ValidationMessages not found. Delegating to org.apache.bval.jsr303.ValidationMessages
14:39:22.157 [localhost-startStop-1] DEBUG o.a.bval.jsr303.xml.ValidationParser - No META-INF/validation.xml found. Using annotation based configuration only.
14:39:24.415 [localhost-startStop-1] ERROR c.m.g.t.TenantDispatcherServlet - Could not add tenant DC.HKG.SALES.VNM, details org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#2' while setting bean property 'sourceList' with key [2]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#2': Cannot resolve reference to bean 'preAuthenticationFilter' while setting constructor argument with key [3]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'preAuthenticationFilter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.m.gsp.utils.WebUtils com.m.gsp.auth.GSPPreAuthenticationFilter.webUtils; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webUtils': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.m.gsp.helper.GSPDelegate com.m.gsp.utils.WebUtils.gspDelegate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gspDelegate': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.m.gsp.service.IWMBService com.m.gsp.helper.GSPDelegate.wmbService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'wmbService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.m.ead.util.spi.config.IProperties com.m.gsp.serviceimpl.WMBServiceImpl.properties; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbPropertyManagerImpl' defined in URL [file:/apache-tomcat-7.0.57/wtpwebapps/m-GSP-Web/WEB-INF/core-context.xml]: Cannot resolve reference to bean 'propertyManagerDAO' while setting bean property 'propertyManagerDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'propertyManagerDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.persistence.EntityManager com.m.gsp.caching.daoimpl.PropertyManagerDAO.entityManager; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManager' defined in URL [file:/apache-tomcat-7.0.57/wtpwebapps/m-GSP-Web/WEB-INF/core-context.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in URL [file:/IT_GSP-Global_Sales_Platform/MTU/Vietnam_UX_Upgrade/DataCenter_HKG/Tenant_VIETNAM/tenant-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: GSP] Unable to build EntityManagerFactory.
May 20, 2015 2:39:24 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
May 20, 2015 2:39:24 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
May 20, 2015 2:39:24 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 21644 ms

我的猜测是我没有正确配置entityManagerFactory bean,但我不确定我哪里出错了。

的persistence.xml:

    <persistence-unit name="GSP" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <class>com.m.gsp.dao.vo.AuditTrailOTO</class>
    <class>com.m.gsp.dao.vo.AuditTrailsOTOPK</class>
    <class>com.m.gsp.dao.vo.GSPPropertiesTO</class>
    <class>com.m.gsp.dao.vo.VariationTO</class>
    <class>com.m.gsp.dao.vo.UserProfileOTO</class>
    <class>com.m.gsp.dao.vo.EConsentMasterOTO</class>
    <class>com.m.gsp.dao.vo.EConsentTransOTO</class>
    <class>com.m.gsp.dao.vo.PageDetailsOTO</class>
    <class>com.m.gsp.dao.vo.EConsentMasterOTO</class>
    <class>com.m.gsp.dao.vo.ProductDetails</class>
    <class>com.m.gsp.dao.vo.ReferenceDataOTO</class>
    <class>com.m.ead.util.health.dao.vo.LoggingEventOTO</class>
    <class>com.m.gsp.prod.dao.vo.CategoryOTO</class>
    <class>com.m.gsp.dao.vo.ProductOTO</class>
    <class>com.m.gsp.prod.dao.vo.CategoryHierarchyOTO</class>
    <class>com.m.gsp.prod.dao.vo.CategoryValueOTO</class>
    <class>com.m.gsp.dao.vo.SubmitAppDetailsOTO</class>
    <class>com.m.gsp.dao.vo.MasterDataCategoryOTO</class>
    <class>com.m.gsp.dao.vo.MasterDataValOTOPK</class>
    <class>com.m.gsp.dao.vo.MasterDataValOTO</class>
    <class>com.m.gsp.dao.vo.MasterDataCategoryOTO</class>
    <class>com.m.gsp.dao.vo.MasterDataValRelOTOPK</class>
    <class>com.m.gsp.dao.vo.MasterDataValRelOTO</class>
    <class>com.m.gsp.customer.dao.vo.CustomerOTO</class>
    <class>com.m.gsp.customer.dao.vo.CustomerVersionOTO</class>
    <class>com.m.gsp.customer.dao.vo.CustomerOTOPK</class>
    <class>com.m.gsp.customer.dao.vo.CustomerVersionOTOPK</class>
    <class>com.m.gsp.entitlement.dao.vo.EntitlementOTO</class>
    <class>com.m.gsp.entitlement.dao.vo.EntitlementOTOPK</class>
    <class>com.m.gsp.dao.vo.KeyValueOTO</class>
    <class>com.m.gsp.dao.vo.CustomerManagement</class>
    <class>com.m.gsp.dao.vo.ApplicationNumOTO</class>
    <class>com.m.gsp.dao.vo.SaveQuoteOTO</class>
    <properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
    <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
</persistence-unit>

租户context.xml中:

    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/salesvietnamMssqlGspDS" resource-ref="true"
    lookup-on-startup="true" />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:persistenceUnitName="GSP" p:dataSource-ref="dataSource" scope="singleton" lazy-init="false">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="false" />
        </bean>
    </property>
</bean>

应用程序的context.xml:

<!-- =========================================  Start: Common JPA config ====================================== -->

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

<bean id="transactionManager" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces" value="org.springframework.transaction.PlatformTransactionManager" />
    <property name="target" ref="txManagerImpl" />
</bean>

<!-- bean id="txManagerImpl" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSource" />
</bean-->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    <property name="url" value="jdbc:sqlserver://server\instance:port;databaseName=GSPVN;" />
    <property name="username" value="un" />
    <property name="password" value="pw" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property -->
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="txManagerImpl" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="persistenceAnnotation" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />  

<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>


<!-- ========================================= Start: SQL Server EntityManagerFactory ========================== -->

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/salesVietnam" resource-ref="true"
    lookup-on-startup="true" />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:persistenceUnitName="GSP" p:dataSource-ref="dataSource" scope="singleton" lazy-init="false">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="false" />
        </bean>
    </property>
</bean>

<!-- ========================================= End: SQL Server EntityManagerFactory ========================== -->

我希望这个例外更具描述性。

  

调用init方法失败;嵌套异常是javax.persistence.PersistenceException:[PersistenceUnit:GSP]无法构建EntityManagerFactory。

我的JDBC资源在tomcat / conf / context.xml中定义如下:

<Resource name="jdbc/salesvietnamMssqlGspDS"
auth="Container"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
type="javax.sql.DataSource"
maxActive="50"
maxIdle="10"
maxWait="15000"
username="un"
password="pw"
url="jdbc:sqlserver://server\instance:port;databaseName=GSPVN;"
removeAbandoned="true"
removeAbandonedTimeout="30"
logAbandoned="true" /> 

我尝试重命名此资源,因此无法找到它,但事实并非如此。抛出的异常有更长的堆栈跟踪和更多细节,但最终以相同的根本原因结束:

  

无法构建EntityManagerFactory

这可能意味着我的资源没有正确定义吗?

1 个答案:

答案 0 :(得分:1)

您正在尝试创建两个具有相同ID的bean。这可能会导致entityManager创建失败。

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/salesVietnam" resource-ref="true"
    lookup-on-startup="true" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    <property name="url" value="jdbc:sqlserver://server\instance:port;databaseName=GSPVN;" />
    <property name="username" value="un" />
    <property name="password" value="pw" />
</bean>

你也有

<bean id="persistenceAnnotation" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />  

如果您可以选择修改XML文件,那么最好使用Annotations将其放入JAVA类中。