when we call save(entity) behind the seen it call persist method of entity manager
and when we update the object it calls merge method on the entity manager.
So same save method is called for persisting and updating the object.
You might be asking this point that why we are calling save method for updating the student ?
So let's go to the update process again and see how the merge method of the entity manager is actually doing update when save method is called. so lets say line 20 has been executed and we have persisted student object into student table. so here both the student and returnedStudent variable the both would be refering to same student object in java memory which would be in a detched state. After that we realised that we have misspelled name alisa we correct that in next time 21 by updating it using the returnedStudent variable but the object being referenced by the returned by the student variable is in the detched state so whatever changes we make to it we will not save to database automatically and will updated only in java memory its like client or user working with detched object rendered on his screen or a web page an example whatever update he makes to detached object unless he saves the detched object
When we delete a record then why an extra select sql statement get executed when we call delete method
Because before deleting student hibernate force check weather this student is exist in our database weather even we have that student in our database or not the way it does that under the hood it calls entity manager. find the student whose id is one.
If we want to delete in batch then we suffix as deleteAllInBatch (InBatch as suffix)
If our database is not a relational db then still we have Interface like JpaRepository like show in below image.
Here you might be thinking why C option is correct because sometimes we don't want to expose all the methods so we can create only the methods that we need it so we can only extend the Repository interface like shown below.
Comments
Post a Comment