gpt4 book ai didi

java - EntityManager 不能使用 persist 将元素保存到数据库

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:59:33 32 4
gpt4 key购买 nike

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

Controller 部分的代码:

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

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

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

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

最佳答案

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

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

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

关于java - EntityManager 不能使用 persist 将元素保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12937394/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com