saveOrUpdate
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 | import org.hibernate.Session; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.service.ServiceRegistry; publicclassEmployeeSaveOrUpdateExample{ publicstaticvoidmain(String[]args){ session.getTransaction().begin(); try{ Employee saveEmp=newEmployee(1,'Kalyan','Developement'); /* * Here employee object with the empno 1 is not there * in database, So it calls save() method and inserts the * session.saveOrUpdate(updateEmp); * Here employee object with the empno 2 is already present * in database. So it calls update() method and updates the * session.getTransaction().commit(); e.printStackTrace(); } publicstaticSession getSession(){ Session session=null; Configuration configuration=newConfiguration(). StandardServiceRegistryBuilder serviceRegistryBuilder= serviceRegistryBuilder.applySettings(configuration.getProperties()); ServiceRegistry serviceRegistry=serviceRegistryBuilder.build(); SessionFactory sessionFactory=configuration.buildSessionFactory session=sessionFactory.openSession(); }catch(Exceptione){ } } |
Difference Between Merge And Saveorupdate In Hibernate
After executing EmployeeSaveOrUpdateExample.java