2015-06-26 51 views
0

我在寫Spring/JPA項目,我需要兩個數據庫的持久單元。當只有一個 - 一切都很好,但是當我添加第二持續性單元,我收到以下錯誤:兩個持久單元和「無法解析與persistence-context-ref-name對應的持久單元」錯誤

2015-06-26 13:19:26.985 [admin-thread-pool-4848(5)] ERROR j.e.s.c.com.sun.enterprise.v3.server - Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method 
2015-06-26 13:19:26.985 [admin-thread-pool-4848(5)] ERROR j.e.s.c.com.sun.enterprise.v3.server - Exception while preparing the app 
2015-06-26 13:19:26.985 [admin-thread-pool-4848(5)] ERROR j.e.s.c.com.sun.enterprise.v3.server - Could not resolve a persistence unit corresponding to the persistence-context-ref-name [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean/entityManager] in the scope of the module called [Q_Management_2]. Please verify your application. 
java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean/entityManager] in the scope of the module called [Q_Management_2]. Please verify your application. 
at com.sun.enterprise.deployment.BundleDescriptor.findReferencedPUViaEMRef(BundleDescriptor.java:694) ~[dol.jar:3.1.2.1-SNAPSHOT] 
at com.sun.enterprise.deployment.BundleDescriptor.findReferencedPUsViaPCRefs(BundleDescriptor.java:682) ~[dol.jar:3.1.2.1-SNAPSHOT] 
at com.sun.enterprise.deployment.WebBundleDescriptor.findReferencedPUs(WebBundleDescriptor.java:1056) ~[dol.jar:3.1.2.1-SNAPSHOT] 
at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:186) ~[jpa-connector.jar:3.1.2.1-SNAPSHOT] 
at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168) ~[jpa-connector.jar:3.1.2.1-SNAPSHOT] 
at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:871) [kernel.jar:3.1.2.1-SNAPSHOT] 
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:410) [kernel.jar:3.1.2.1-SNAPSHOT] 
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240) [kernel.jar:3.1.2.1-SNAPSHOT] 
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:389) [deployment-admin.jar:3.1.2.1-SNAPSHOT] 
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:348) [kernel.jar:3.1.2.1-SNAPSHOT] 
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:363) [kernel.jar:3.1.2.1-SNAPSHOT] 
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1085) [kernel.jar:3.1.2.1-SNAPSHOT] 
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:95) [kernel.jar:3.1.2.1-SNAPSHOT] 
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1291) [kernel.jar:3.1.2.1-SNAPSHOT] 
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1259) [kernel.jar:3.1.2.1-SNAPSHOT] 
at org.glassfish.admin.rest.ResourceUtil.runCommand(ResourceUtil.java:214) [rest-service.jar:3.1.2.1-SNAPSHOT] 
at org.glassfish.admin.rest.ResourceUtil.runCommand(ResourceUtil.java:207) [rest-service.jar:3.1.2.1-SNAPSHOT] 
at org.glassfish.admin.rest.resources.TemplateListOfResource.createResource(TemplateListOfResource.java:148) [rest-service.jar:3.1.2.1-SNAPSHOT] 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_67] 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_67] 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_67] 
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_67] 
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.uri.rules.SubLocatorRule.accept(SubLocatorRule.java:134) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.uri.rules.SubLocatorRule.accept(SubLocatorRule.java:134) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339) [jersey-gf-server.jar:1.11.1] 
at com.sun.jersey.server.impl.container.grizzly.GrizzlyContainer._service(GrizzlyContainer.java:182) [jersey-grizzly.jar:1.11.1] 
at com.sun.jersey.server.impl.container.grizzly.GrizzlyContainer.service(GrizzlyContainer.java:147) [jersey-grizzly.jar:1.11.1] 
at org.glassfish.admin.rest.adapter.RestAdapter.service(RestAdapter.java:148) [rest-service.jar:3.1.2.1-SNAPSHOT] 
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179) [grizzly-utils.jar:1.9.50] 
at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117) [kernel.jar:3.1.2.1-SNAPSHOT] 
at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354) [kernel.jar:3.1.2.1-SNAPSHOT] 
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) [kernel.jar:3.1.2.1-SNAPSHOT] 
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860) [grizzly-http.jar:1.9.50] 
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757) [grizzly-http.jar:1.9.50] 
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056) [grizzly-http.jar:1.9.50] 
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229) [grizzly-http.jar:1.9.50] 
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) [grizzly-framework.jar:1.9.50] 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) [grizzly-framework.jar:1.9.50] 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) [grizzly-framework.jar:1.9.50] 
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) [grizzly-http.jar:1.9.50] 
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) [grizzly-framework.jar:1.9.50] 
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) [grizzly-framework.jar:1.9.50] 
at com.sun.grizzly.ContextTask.run(ContextTask.java:71) [grizzly-framework.jar:1.9.50] 
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) [grizzly-utils.jar:1.9.50] 
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) [grizzly-utils.jar:1.9.50] 
at java.lang.Thread.run(Thread.java:745) [na:1.7.0_67] 
2015-06-26 13:19:27.173 [admin-thread-pool-4848(5)] ERROR j.e.s.t.a.o.g.deployment.admin - Exception while preparing the app : Could not resolve a persistence unit corresponding to the persistence-context-ref-name [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean/entityManager] in the scope of the module called [Q_Management_2]. Please verify your application. 

的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" 
     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_2_0.xsd"> 

<persistence-unit name="qp_centralPU" transaction-type="JTA"> 
    <jta-data-source>jdbc/CentralPool</jta-data-source> 
    <properties> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect" /> 
     <property name="hibernate.connection.url" value="jdbc:jtds:sqlserver://192.168.0.68:1433" /> 
     <property name="hibernate.connection.driver_class" value="net.sourceforge.jtds.jdbc.Driver" /> 
     <property name="hibernate.connection.username" value="qp_central" /> 
     <property name="hibernate.connection.password" value="qp_central" /> 
     <property name="hibernate.show_sql" value="true" /> 
     <property name="SQLServerDialect" value="org.hibernate.dialect.SQLServer2008Dialect" /> 
     <property name="hibernate.transaction.manager_lookup_class" 
        value="org.hibernate.transaction.SunONETransactionManagerLookup"/> 
     <property name="hibernate.dialect_resolvers" value="com.qmatic.qp.dialect.UnicodeSQLServer2008DialectResolver" /> 
     <property name="hibernate.HibernateException" value="org.hibernate.HibernateException" /> 
    </properties> 
</persistence-unit> 

<persistence-unit name="qp_statPU" transaction-type="JTA"> 
    <jta-data-source>jdbc/StatPool</jta-data-source> 
    <properties> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect" /> 
     <property name="hibernate.connection.url" value="jdbc:jtds:sqlserver://192.168.0.68:1433" /> 
     <property name="hibernate.connection.driver_class" value="net.sourceforge.jtds.jdbc.Driver" /> 
     <property name="hibernate.connection.username" value="stat" /> 
     <property name="hibernate.connection.password" value="stat" /> 
     <property name="hibernate.show_sql" value="true" /> 
     <property name="SQLServerDialect" value="org.hibernate.dialect.SQLServer2008Dialect" /> 
     <property name="hibernate.transaction.manager_lookup_class" 
        value="org.hibernate.transaction.SunONETransactionManagerLookup" /> 
     <property name="hibernate.dialect_resolvers" value="com.qmatic.qp.dialect.UnicodeSQLServer2008DialectResolver" /> 
     <property name="hibernate.HibernateException" value="org.hibernate.HibernateException" /> 
    </properties> 
</persistence-unit> 

的applicationContext.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:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/jdbc 
    http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd 
    http://www.springframework.org/schema/cache 
    http://www.springframework.org/schema/cache/spring-cache.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 

<context:annotation-config /> 

<context:component-scan base-package="com.qsystems.Repository, com.qsystems.DAL, com.qsystems.controllers" /> 
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> 

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

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

<bean id="jpaDialect" 
     class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> 

<bean id="CentralEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSourceCentral"/> 
    <property name="loadTimeWeaver"> 
     <bean class="org.springframework.instrument.classloading.glassfish.GlassFishLoadTimeWeaver"/> 
    </property> 
    <property name="persistenceUnitName" value="qp_centralPU" /> 
    <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"/> 
    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop> 
      <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.SunONETransactionManagerLookup</prop> 
     </props> 
    </property> 
    <property name="jpaDialect" ref="jpaDialect" /> 
</bean> 


<bean id="StatEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSourceStat"/> 
    <property name="loadTimeWeaver"> 
     <bean class="org.springframework.instrument.classloading.glassfish.GlassFishLoadTimeWeaver"/> 
    </property> 
    <property name="persistenceUnitName" value="qp_statPU" /> 
    <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"/> 
    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop> 
      <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.SunONETransactionManagerLookup</prop> 
     </props> 
    </property> 
</bean> 

<bean id="dataSourceCentral" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" /> 
    <property name="url" value="jdbc:jtds:sqlserver://localhost:1433" /> 
    <!--<property name="url" value="jdbc:jtds:sqlserver://192.168.0.68:1433" />--> 
    <property name="username" value="qp_central" /> 
    <property name="password" value="qp_central" /> 
</bean> 

<bean id="dataSourceStat" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" /> 
    <property name="url" value="jdbc:jtds:sqlserver://localhost:1433" /> 
    <property name="username" value="stat" /> 
    <property name="password" value="stat" /> 
</bean> 

連接池:

Resource Type: javax.sql.XADataSource 
Datasource Classname: com.microsoft.sqlserver.jdbc.SQLServerXADataSource 

DAO類:

@Repository 
@Transactional(value = "CentralTransactionManager") 
public class BranchDAO { 

    protected EntityManagerFactory emf; 
    private EntityManager em; 

    @PersistenceContext(unitName = "qp_centralPU") 
    @Qualifier(value = "CentralEmf") 
    public void setEntityManager(EntityManager em) 
    { 
     this.em = em; 
    } 

    public Branch getBranchById(long Id) 
    { 
     return em.find(Branch.class, Id); 
    } 
} 

請幫幫忙!

+0

你如何檢索你的類中的'EntityManager'?我想還沒有在那裏指定PersistenceUnit ... – unwichtich

+0

將你的'peristence.xml'重命名爲別的東西,並明確聲明哪一個用於'LocalContainerEntityManagerFactory'。 「persistence.xml」的存在觸發GlassFish掃描「@PersistenceUnit」和「@PersistenceContext」註釋,並將應用程序視爲EJB3應用程序。 –

+0

@ unwichtich,'public class BranchGroupDAO {0} { }受保護的EntityManagerFactory emf; private EntityManager em; @PersistenceUnit(的unitName = 「qp_centralPU」) @Qualifier(值= 「CentralEmf」) 公共無效setEntityManagerFactory(EntityManagerFactory的電動勢){ this.emf =電動勢; em = emf.createEntityManager(); } public List getAllBranchGroups(){ List brList = null; brList = em.createQuery(「SELECT b FROM BranchGroup b」)。getResultList(); return brList; } } ' –

回答

0

我在一個JPA + Spring + Glassfish項目中遇到了兩個持久單元的相同錯誤。

有兩個提示該結合導致了成功:

第一個是this,並且第二個是this

相關問題