简体   繁体   中英

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 .

My code is working as expected, however when I call repository.save() the database is not altered?

Do I also need to then call a commit after this in order to alter the Database? Or should the repository.save() method be altering the database automatically?

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".

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.

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; commit() is a method of the transaction associated to the Entity Manager.

In some frameworks like play 1.4.x the commits is issued after the response view is correctly parsed and rendered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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