【问题标题】:EntityManager cannot use persist to save element to databaseEntityManager 不能使用持久保存元素到数据库
【发布时间】:2012-10-07 21:25:17
【问题描述】:

我遇到了使用 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>

【问题讨论】:

  • 你试过在persist方法上使用@Transactional吗?
  • 对不起,我忘了在帖子里提到它。是的,我以所有四种方式在方法之前添加了@Transactional。另外我尝试在事务注释之后添加(readOnly = true)/(readOnly = false),甚至还添加了(propagation = Propagation.REQUIRED),但似乎没有区别。
  • 你是在使用@PersistenceContext 来注入entityManager 吗?
  • 是的,我在控制器的开头添加了它:@PersistenceContext private EntityManager entityManager;
  • 如果你使用注解,你的 Spring 上下文中是否有这个 以及定义注入实体管理器的事务管理器?

标签: java spring hibernate entitymanager


【解决方案1】:

要在 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>

【讨论】:

  • 我发现我的spring上下文中的注解是这样的:这里面应该修改吗?跨度>
  • 尝试删除模式参数以使用默认模式="proxy"
  • 哇非常感谢@JustinKSU 我现在解决了这个问题!这正是注释的问题!也许我应该在下面附上我的 spring 上下文的内容,以帮助其他可能也遇到类似问题的人。无论如何,非常感谢您的帮助!
  • @JustinKSU 我按照您的回答尝试了请参阅pastie.org/9977330,当我运行它时,我会收到类似的错误pastie.org/9977329
  • @SpringLearner 我建议您提出自己的问题,社区可能会为您提供帮助。评论不适用于这样的事情。如果您认为相关,可以在您的问题中链接此答案。在我的脑海中,一个常见的问题是 Spring 版本的混合。
【解决方案2】:

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

【讨论】:

  • 根据 OP 帖子中的代码 sn-ps,所有方法都是 public。它还指出,添加 &lt;tx:annotation-driven /&gt; 时可以解决问题。我没有看到您的回答对讨论有任何帮助。
  • 也许这个答案可以帮助其他开发者?!
猜你喜欢
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多