Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在@PersistenceContext中设置unitName的Spring Hibernate配置_Java_Spring_Hibernate_Jpa - Fatal编程技术网

Java 在@PersistenceContext中设置unitName的Spring Hibernate配置

Java 在@PersistenceContext中设置unitName的Spring Hibernate配置,java,spring,hibernate,jpa,Java,Spring,Hibernate,Jpa,我在这里和网上看到了一些关于这方面的问题,但它们似乎与我收到的错误消息不太相符 我一直在代码中使用JPA注释来处理数据库。我使用@PersistenceContext注释来配置实体管理器。在我将多个持久性单元添加到持久性xml之前,这一切都很好。然后我想打电话 @PersistenceContext(unitName = "myPU") 然后我发现问题是没有找到名为myPU的bean 实际上,我已经从persistence.xml中删除了第二个持久性单元,我只是尝试基本上按名称引用我的一个持久

我在这里和网上看到了一些关于这方面的问题,但它们似乎与我收到的错误消息不太相符

我一直在代码中使用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>
目前我正在尝试部署到Glassfish

有人能帮忙吗?我确信我在这里遗漏了一些基本的东西

谢谢

编辑

我尝试了主人奴隶给出的答案,但不幸的是,这不起作用

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

我清理了core-context.xml文件并删除了几行不需要的内容,从而成功地实现了这一点。我觉得特别有用

<?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>

@PersistenceContext(unitName = "myPU", name="jdbc/mycore")
<?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>