尝试将@PersistenceContext与unitName一起使用时出现异常

时间:2012-08-01 12:06:29

标签: spring jpa java-ee-6 jboss7.x

我使用的是Spring 3.1,JPA 2和Hibernate,JBoss。我想使用多个持久性单元。这是我的persistence.xml

    <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="pu1">
          <jta-data-source>java:jboss/datasources/ds1</jta-data-source>
          <properties>
             <property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/em1" />
             <!-- Properties for Hibernate -->
             <property name="hibernate.hbm2ddl.auto" value="create-drop" />
             <property name="hibernate.show_sql" value="true" />
          </properties>
       </persistence-unit>
<persistence-unit name="pu2">
          <jta-data-source>java:jboss/datasources/ds2</jta-data-source>
          <properties>
             <property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/em2" />
             <!-- Properties for Hibernate -->
             <property name="hibernate.hbm2ddl.auto" value="create-drop" />
             <property name="hibernate.show_sql" value="true" />
          </properties>
       </persistence-unit>
    </persistence>

当我尝试使用unitName创建EntityManager时,就像这样

@PersistenceContext(unitName="pu1")

我得到例外,

16:56:35,834 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-1) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'languageDAOImpl': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'pu1' is defined
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:342) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 

我做错了什么?如果我只有一个持久性单元,并且如果我使用@PersistenceContext而没有unitName则它正常工作。

我不知道如何设置和使用多个持久性单元。

0 个答案:

没有答案