简体   繁体   English

Spring数据jpa在普通的java应用程序中提交?

[英]Spring data jpa commit in normal java application?

When using spring and spring data jpa in a normal java application how will jpa know when to commit the data to the database? 在普通的java应用程序中使用spring和spring数据时,jpa如何知道何时将数据提交到数据库?

I ask this because repository.save() doesn't commit. 我问这个是因为repository.save()没有提交。

Should i manually commit or can spring handle this for me? 我应该手动提交还是可以为我处理这个问题?

My application context xml: 我的应用程序上下文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" />

I manually init my context with: 我手动初始化我的上下文:

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

No spring can handle transaction for you just before your method repository.save() add @Transactional 在方法repository.save()添加@Transactional之前,没有spring可以为您处理事务

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

and in your configuration class make sure that spring transaction handling is enabled 并在您的配置类中确保启用了Spring事务处理

@EnableTransactionManagement
class myConfig{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM