Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

java - EntityManager cannot use persist to save element to database

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:

Code in Controller part:

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

1. DaoJpa:

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

Doesn't work!

2.

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

Exception I got: no transaction is in progress

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

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

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:

<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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Appropriate for your version of 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...