Spring Hibernate配置,在@PersistenceContext中设置unitName

时间:2012-12-20 21:39:47

标签: java spring hibernate jpa

我在这里和网上看到过一些关于此的问题,但它们似乎与我得到的错误信息完全匹配。

我一直在我的代码中使用JPA注释来处理数据库。我使用@PersistenceContext批注来配置实体管理器。这一切都很好,直到我向持久性xml添加多个持久性单元。然后我想打电话给

@PersistenceContext(unitName = "myPU")

然后我遇到问题,说找不到名为myPU的bean

我已经从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>

我的core-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")

1 个答案:

答案 0 :(得分: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>