2012-12-20 33 views
4

我在這裏和網上看到了一些關於這個的問題,但它們看起來不太符合我得到的錯誤消息。在@PersistenceContext中設置unitName的Spring Hibernate配置

我一直在我的代碼中使用JPA註釋來處理數據庫。我使用@PersistenceContext註釋來配置實體管理器。這一切運行良好,直到我添加多個持久性單元到我的持久性xml。然後,我想打電話給

@PersistenceContext(unitName = "myPU") 

然後我得到的問題,說與名字myPU無豆發現

我已經actaully從我的persistence.xml去除第二持久性單元和我只是想基本上引用我的名字是一個持久性單元(我知道這不是必須的,但是一旦我添加了另一個pu就會被使用)。

我的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="myPU" transaction-type="JTA"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <jta-data-source>jdbc/mycore</jta-data-source> 
    <exclude-unlisted-classes>false</exclude-unlisted-classes> 
    <properties> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/> 
    </properties> 
</persistence-unit> 

和我的核心是context.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:tx="http://www.springframework.org/schema/tx" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"> 

<context:annotation-config/> 

<context:component-scan base-package="com.mine.model"/> 
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"> 
    <property name="persistenceUnits"> 
     <map> 
      <entry key="myPU" value="jdbc/mycore"/> 

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

<tx:jta-transaction-manager/> 
<tx:annotation-driven/> 

<jee:jndi-lookup id="entityManagerFactory" jndi-name="jdbc/mycore"/> 

確切的錯誤我得到的是

Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myfirstbean': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myPU' is defined. Please see server.log for more details. 

目前我正在嘗試部署到Glassfish。

任何人都可以幫忙嗎?我敢肯定,我失去了一些東西基本相當這裏

感謝

編輯

我試圖MasterSlave給出了答案,這不幸的是沒有工作。

@PersistenceContext(unitName = "myPU", name="jdbc/mycore") 

回答

2

我設法通過清理core-context.xml文件並刪除幾行不需要的行來實現這個功能。我發現this特別有用

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" ... and some more stuff> 

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

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> 
    <tx:jta-transaction-manager/> 
    <tx:annotation-driven/> 

    <jee:jndi-lookup id="corePU" jndi-name="jdbc/mycore"/> 
</beans> 
相關問題