I am wondering what is the best practice for updating a record using JPA? Currently, I have developed my own sample, but I suspect that this is by no means the best practice. What I'm doing is essentially looking to see if there is an entry in db, if I cannot find it, I call the method enityManager.persist(object<T>). if it exists, I call the method entityManager.Merge(Object<T>).
The reason I ask is because I found out that the merge method shows if there is an entry in the allready database, and if it is not in db, then it continues to add it, if it does, it makes the necessary changes. In addition, you need to attach a merge call in getTransaction (). Begin () and getTransaction.commit ()? Here is what I still have ...
try{
launchRet = emf.find(QuickLaunch.class, launch.getQuickLaunchId());
if(launchRet!=null){
launchRet = emf.merge(launch);
}
else{
emf.getTransaction().begin();
emf.persist(launch);
emf.getTransaction().commit();
}
}