【问题标题】:Spring data jpa commit in normal java application?普通Java应用程序中的Spring数据jpa提交?
【发布时间】:2017-02-22 12:09:14
【问题描述】:

当在普通的java应用程序中使用spring和spring data jpa时,jpa如何知道何时将数据提交到数据库?

我问这个是因为 repository.save() 没有提交。

我应该手动提交还是可以由 spring 为我处理?

我的应用程序上下文 xml:

 <context:component-scan base-package="...">
    </context:component-scan>

<jpa:repositories base-package="..." entity-manager-factory-ref="emf"  />



<bean id="emf" class="...spring.MyEMF"></bean>

<!-- Add Transaction support -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="emf" />
</bean>

<!-- Use @Transaction annotations for managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />

我手动初始化我的上下文:

new ClassPathXmlApplicationContext("classpath:/data.xml")

【问题讨论】:

  • 尝试使用repository.saveAndFlush()

标签: java spring spring-data spring-data-jpa


【解决方案1】:

在你的方法 repository.save() 添加 @Transactional 之前没有 spring 可以为你处理事务

@Transactional
public void save(){
   //do something
}

并在您的配置类中确保启用了 spring 事务处理

@EnableTransactionManagement
class myConfig{

}

【讨论】:

  • 嗯,我好像错了,save() 确实提交了。似乎像 save 这样的spring data jpa crud方法默认是@transactional。我已启用记录器 org.springframework.orm.jpa.JpaTransactionManager 以查看详细信息。我需要做一些研究。很快就会回来。
猜你喜欢
  • 1970-01-01
  • 2011-10-30
  • 2016-05-17
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
  • 1970-01-01
  • 2020-12-21
  • 2016-08-04
相关资源
最近更新 更多