Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java EntityManager无法使用persist将元素保存到数据库_Java_Spring_Hibernate_Entitymanager - Fatal编程技术网

Java EntityManager无法使用persist将元素保存到数据库

Java EntityManager无法使用persist将元素保存到数据库,java,spring,hibernate,entitymanager,Java,Spring,Hibernate,Entitymanager,我遇到了使用EntityManager将元素持久化到数据库的问题。根据我找到的答案,我在DaoJpa中尝试了这4种方法来做这样的事情,但还是失败了。在此,我附上了我尝试过的四种方法: 控制器部分中的代码: @Transactional SmartProduct smartProduct = new SmartProduct(); smartProduct.setName("Dove Soap"); smartProductDao.pe

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

控制器部分中的代码:

   @Transactional 
   SmartProduct smartProduct = new SmartProduct();
            smartProduct.setName("Dove Soap");
            smartProductDao.persist(smartProduct);
一,。 DaoJpa:

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

二,

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

三,

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

四,

我得到的例外: 应用程序必须提供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>

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


如果您仍然存在此问题,并且所有配置都正常,请确保@Transaction注释的方法是公共的,不受保护,以便由事务管理器识别/管理。

您是否尝试在persist方法上使用@Transactional?抱歉,我忘了在帖子中提到它。是的,我在方法之前添加了@Transactional,这四种方式都是如此。此外,我还尝试在事务注释之后添加(readOnly=true)/(readOnly=false),甚至还添加了(propagation=propagation.REQUIRED),但似乎没有什么区别。是否使用@PersistenceContext注入entityManager?是的,我在控制器的开头添加了它:@PersistenceContext private entityManager entityManager;如果您使用的是批注,您在Spring上下文中是否有此项以及定义transactionManager注入实体管理器?我发现我的Spring上下文中的批注是这样的:我应该在此修改任何批注吗?请尝试使用默认的mode=“proxy”删除mode参数哇,非常感谢@JustinKSU我现在就解决问题!这正是注释的问题!也许我应该在下面附上我的spring上下文的内容,以帮助其他可能遇到类似问题的人。无论如何,非常感谢你的帮助@JustinKSU我按照你的答案试过了,请看,当我运行它时,我得到了类似于它的错误@SpringLearner,我建议你问自己的问题,社区可能会帮助你。评论不是针对这样的事情。在你的问题中,如果你觉得这个答案是相关的,你可以把它链接起来。在我看来,一个常见的问题是混合使用Spring版本。根据OP文章中的代码片段,所有方法都是
public
。此外,它还指出,当添加
时,解决了问题。我看不到你的答案在讨论中添加任何内容。也许这个答案可以帮助其他开发人员?!
<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>