Issue
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?
Solution
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.
Answered By - Hans Poo