Create a new student object
Get an entity manager instance from the persistence.xml file
Save a student object using persist()
Output:
name -> "Saman"
age -> 24
When we call persist on that new student instance, It will do following things.
1. Object added to persistence context as new entity(Make an instance managed)
2. New entity inserted into database at flush/commit
Save a student object using merge()
Output:
name -> "Saman"
age -> 12
1. State copied to new entity.
2. New entity added to persistence context
3. New entity inserted into database at flush/commit
4. New entity returned
So if we want to update new entity using merge(), we can do something like this.
Output:
name -> "Saman"
age -> 24
Git url for demo project: https://github.com/arlahiru/JPADemo
Following links helped me to understand this difference and all the credits should go to its authors.
http://stackoverflow.com/questions/1069992/jpa-entitymanager-why-use-persist-over-merge
http://java.boot.by/scbcd5-guide/ch06.html
http://planet.jboss.org/post/get_started_quickly_with_hibernate_annotations_and_jpa_2
http://www.noway.es/JPA-eclipselink-helloworld
Hope this helps you. See you next time. Bye! :D