我在这里和网上看到了一些关于此的问题,但它们似乎与我收到的错误消息不太匹配。
我一直在我的代码中使用 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")