EntityManager不能使用persist将元素保存到数据库 [英] EntityManager cannot use persist to save element to database
问题描述
我遇到了使用EntityManager将元素持久化到数据库的问题。根据我发现的答案,我在DaoJpa尝试了这4种方法来做这样的事情,但仍然失败。在这里,我附加了我尝试的四种方式:
控制器部分中的代码:
@Transactional
SmartProduct smartProduct = new SmartProduct();
smartProduct.setName(Dove Soap);
smartProductDao.persist(smartProduct);
1。
DaoJpa:
@Transactional
public void persist(SmartProduct smartProduct){
entityManager。坚持(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.JpaTransactionManagerid =transactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>
< tx:注解驱动模式=aspectjtransaction-manager =transactionManager/>
< bean class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBeanid =entityManagerFactory>
< property name =persistenceUnitNamevalue =persistenceUnit/>
< property name =dataSourceref =dataSource/>
< / bean>
加入
< tx:annotation-driven />
有效:
< tx:annotation-driven />
< bean class =org.springframework.orm.jpa.JpaTransactionManagerid =transactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>
< tx:注解驱动模式=aspectjtransaction-manager =transactionManager/>
< bean class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBeanid =entityManagerFactory>
< property name =persistenceUnitNamevalue =persistenceUnit/>
< property name =dataSourceref =dataSource/>
< / bean>
要在Spring上下文中启用@Transactional,如下所示:
适合您的Spring版本:
< ; beans xmlns =http://www.springframework.org/schema/beansxmlns: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 =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>
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>
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>
这篇关于EntityManager不能使用persist将元素保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!