6

我遇到了使用 EntityManager 将元素持久化到数据库的问题。根据我找到的答案,我在我的 DaoJpa 中尝试了这 4 种方法来做这样的事情,但仍然失败。在这里,我附上了我尝试过的四种方法:

控制器部分的代码:

   @Transactional 
   SmartProduct smartProduct = new SmartProduct();
            smartProduct.setName("Dove Soap");
            smartProductDao.persist(smartProduct);

1.道帕:

 @Transactional
 public void persist(SmartProduct smartProduct) {
            entityManager.persist(smartProduct);

不工作!

2.

@Transactional
public void persist(SmartProduct smartProduct) {
entityManager.persist(smartProduct);
entityManager.flush();

我得到的例外:没有交易正在进行中

3.

@Transactional
public void persist(SmartProduct smartProduct) {
EntityTransaction emTransaction = entityManager.getTransaction();
        emTransaction.begin();  
        entityManager.persist(smartProduct);
        emTransaction.commit();
        entityManager.close();

我得到的异常:不允许在共享 EntityManager 上创建事务 - 改用 Spring 事务或 EJB CMT

4.

@Transactional
public void persist(SmartProduct smartProduct) {
                    EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");
                EntityManager em = emf.createEntityManager();
                EntityTransaction etx = em.getTransaction();
                etx.begin();
                em.persist(smartProduct);
                etx.commit();
                em.close();
                emf.close();

我得到的异常:应用程序必须提供 JDBC 连接

有人可以帮我解决问题吗?提前谢谢了!

非常感谢 JustinKSU 的帮助。我在 Spring 上下文中添加了注释,然后它就解决了!这是我的 Spring 上下文的先前版本:

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

    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />

    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
        <property name="persistenceUnitName" value="persistenceUnit" />
        <property name="dataSource" ref="dataSource" />
    </bean>

添加后

<tx:annotation-driven />

有用:

<tx:annotation-driven />
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />

    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
        <property name="persistenceUnitName" value="persistenceUnit" />
        <property name="dataSource" ref="dataSource" />
    </bean>
4

2 回答 2

9

要在 Spring 上下文中启用 @Transactional,您应该具有以下内容:

适合您的 Spring 版本:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

启用注释:

<tx:annotation-driven />

声明你的事务管理器注入你的实体管理器:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
于 2012-10-17T15:21:56.857 回答
1

如果您仍然有这个问题并且所有配置都正常,请确保@Transaction注解的方法是公共的,不受保护以便被事务管理器识别/管理。

于 2014-09-16T20:06:04.003 回答