简体   繁体   English

Spring JPA:Save() 方法是否应该将数据提交到数据库?

[英]Spring JPA: Should the Save() method commit data to the database?

I am using Spring data for my project, I am using a Standard Repository that extends CRUD Repository .我正在为我的项目使用Spring data ,我使用的是extends CRUD Repository的标准Repository extends CRUD Repository

My code is working as expected, however when I call repository.save() the database is not altered?我的代码按预期工作,但是当我调用repository.save() ,数据库没有改变?

Do I also need to then call a commit after this in order to alter the Database?我是否还需要在此之后调用commit以更改数据库? Or should the repository.save() method be altering the database automatically?或者repository.save()方法是否应该自动更改数据库?

When your application runs, the Entity Manager associated with the thread keeps control of modified or added objects, the save() method just do this, it's a mark that says: "this should be saved in the database".当您的应用程序运行时,与线程关联的实体管理器保持对修改或添加对象的控制,save() 方法只是这样做,它是一个标记,表示:“这应该保存在数据库中”。

The database DML (insert,update,delete) is not sent to the database while you save things, it's done at the end just before commit, it's delayed to the last moment.数据库 DML(插入、更新、删除)不会在您保存内容时发送到数据库,它会在最后提交之前完成,它会延迟到最后一刻。

It's possible the send the DML to the database anytime you like using the Entity Manager's flush() method, in fact you can debug the database log and see your queries going through, but this changes to the database will only be visible within your DB connection until the commit is issued;可以随时使用实体管理器的 flush() 方法将 DML 发送到数据库,实际上您可以调试数据库日志并查看查询的过程,但是对数据库的这种更改仅在您的数据库连接中可见直到提交提交; commit() is a method of the transaction associated to the Entity Manager. commit() 是与实体管理器关联的事务的方法。

In some frameworks like play 1.4.x the commits is issued after the response view is correctly parsed and rendered.在像 play 1.4.x 这样的一些框架中,提交是在响应视图被正确解析和呈现后发出的。

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

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