简体   繁体   English

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

[英]Spring Hibernate configuration with setting unitName in @PersistenceContext

I have seen a few questions here and online about this but they dont seem to quite match the error message I am getting. 我在这里和在线上已经看到了一些关于此的问题,但它们似乎与我收到的错误消息不太匹配。

I have been using JPA annotations within my code to deal with the database. 我一直在代码中使用JPA批注来处理数据库。 I use the @PersistenceContext annotation to configure the entity manager. 我使用@PersistenceContext批注来配置实体管理器。 This all works well until I add more than one persistence unit to my persistence xml. 在我向持久性xml中添加多个持久性单元之前,所有方法都可以正常工作。 I then want to call 然后我想打电话

@PersistenceContext(unitName = "myPU")

I was then getting problems saying that no bean with name myPU was found 然后我遇到问题,说找不到名为myPU的bean

I have actaully removed the second persistence unit from my persistence.xml and am just trying to basically reference my one persistence unit by name (I know this is not needed but will be once I add another pu). 我已经从persistence.xml中实际删除了第二个持久性单元,而只是试图基本上按名称引用我的一个持久性单元(我知道这不是必需的,但是一旦添加另一个pu就会使用)。

My persistence.xml is 我的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>

and my core-context.xml is 而我的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"/>

The exact error I get is 我得到的确切错误是

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. 

Currently I am trying to deploy to Glassfish. 目前,我正在尝试部署到Glassfish。

Can anyone help? 有人可以帮忙吗? Im sure I am missing something quite basic here 我确定我在这里缺少一些基本的东西

Thanks 谢谢

EDIT 编辑

I tried the answer MasterSlave gave and this unfortunately did not work. 我尝试使用MasterSlave给出的答案,但不幸的是,此方法不起作用。

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

I managed to get this going by cleaning out my core-context.xml file and removing several lines that were not needed. 我设法通过清除core-context.xml文件并删除了不需要的几行来实现这一目标。 I found this particularly useful 我发现特别有用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM