java.lang.IllegalStateException:没有可用的事务性EntityManager

时间:2013-01-25 13:24:30

标签: spring hibernate maven jpa

项目使用Hibernate(JPA),Spring和Maven。我的实体和DAO在一个单独的JAR中。

的pom.xml:

<project ...>
    ...
    <artifactId>database</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.5.4-Final</version>
        </dependency>
    </dependencies>    
</project>

DAO:

public class AbstractDAO<T extends BaseEntity> implements GenericDAO<T> {


    private final Class<T> persistentClass;

    private EntityManager entityManager;

    public AbstractDAO(Class<T> entityClass) {
        super();
        this.persistentClass = entityClass;
    }

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }


    public EntityManager getEntityManager() {
        return entityManager;
    }

    ...

    public void fooBar() {
       //Exception from this line
       Session session = getEntityManager().unwrap(Session.class);
       ...
    }

    ....

}

我有一个使用Spring的模块。

的pom.xml:

<project ...>
...
<artifactId>api</artifactId>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    ....
</dependencies>

 ...    
</project>

AppContext.xml:

<bean id="authService" scope="singleton" class="com.test.management.AuthServiceImpl" />

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" name="EntityManagerFactory">
        <property name="persistenceUnitName" value="default"></property>
        <property name="dataSource" ref="dataSource"></property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
                <property name="databasePlatform" value="${db.dialect}" />
            </bean>
        </property>     
    </bean>

    <!-- Values are defined in db.properties -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" name="TransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    </bean>

    <tx:annotation-driven />

    <bean id="userDAO" scope="singleton" class="com.test.database.dao.impl.UserDAOImpl">
    </bean>


    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

</beans> 

服务:

public class AuthServiceImpl implements AuthService {

    @Autowired
    private UserDAO userDAO;


    @Override
    public void authorization() {
        userDAO.fooBar();

    }
}

当我试图从EntityManager获取会话时,我发现了这个异常:

java.lang.IllegalStateException: No transactional EntityManager available
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:223)
    at $Proxy121.unwrap(Unknown Source) 

4 个答案:

答案 0 :(得分:37)

您必须使用@Transactional注释包围该方法:

@Transactional
public void fooBar() {
   //Exception from this line
   Session session = getEntityManager().unwrap(Session.class);
   ...
}

在spring的xml配置文件中使用以下声明启用spring @Transactional处理(txManager是你的经理的id)。

<tx:annotation-driven transaction-manager="txManager" />

答案 1 :(得分:20)

试试这个?

entityManager=entityManager.getEntityManagerFactory().createEntityManager();
Session session = (Session) entityManager.unwrap(Session.class);

答案 2 :(得分:3)

这些都不适合我,我终于发现问题是我正在制作我的方法@Transactional而不是我需要这个类是@Transactional

答案 3 :(得分:0)

对于我来说,这是另一个问题(对于@Transactional有问题的人):

@Transactional 起作用需要动态代理才能起作用。正是基于接口需要它的代理(JDK代理。在这种情况下CGLIB不起作用)。因此,通过服务创建接口并实现它, @Transactional 批注将起作用。