java EntityManager 不能使用持久化将元素保存到数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12937394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:51:12  来源:igfitidea点击:

EntityManager cannot use persist to save element to database

javaspringhibernateentitymanager

提问by Qingshan Zhang

I met the problem of persisting element to database using EntityManager. Based on the answers I found, I tried those 4 ways in my DaoJpa to do such thing but still failed. Here I attached the four ways I tried:

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

Code in Controller part:

控制器部分的代码:

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

1. DaoJpa:

1. 道帕:

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

Doesn't work!

不起作用!

2.

2.

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

Exception I got: no transaction is in progress

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

3.

3.

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

Exception I got: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead

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

4.

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();

Exception I got: The application must supply JDBC connections

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

Could someone help me figure out the problem please? Many thanks in advance!

有人可以帮我找出问题吗?提前谢谢了!

Many thanks JustinKSU's help. I add the annotation in Spring context and then it solved! Here is the previous version of my Spring context:

非常感谢 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>

After added the

添加后

<tx:annotation-driven />

it works:

有用:

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

回答by JustinKSU

To enable @Transactional in your Spring context you should have the following:

要在 Spring 上下文中启用 @Transactional,您应该具备以下条件:

Appropriate for your version of Spring:

适用于您的 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">

Enable the annotations:

启用注释:

<tx:annotation-driven />

Declare your transaction manager injecting your entity manager:

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

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

回答by Criss

If you still have this problem and all the configurations are ok, please make sure that the @Transaction annotated method is public, not protected in order to be identified/managed by the transaction manager.

如果还是有这个问题,并且所有的配置都OK,请确保@Transaction注解的方法是public的,没有protected,以便事务管理器识别/管理。