Three states and changes of entity objects

1. Transient state (new object)-----call the save() method to store the data, and the transient state becomes the persistent state

     User user = new User();

2. Persistent state (corresponding records in the database and associated with the session)

     Configuration cfg = new Configuration();
     SessionFactory sf = cfg.configure().buildSessionFactory();
     Session s = sf.openSession();
     Transaction ts = s.beginTransaction();
     User user = new User();
     user.setUserName("root"+new Date());
     user.setPassword("123");
     s.save(user);
     user.setPassword("new123");
     ts.commit();
     s.close();

3. Detached state (there is a corresponding record in the database, but it is not associated with the session) ----update() method, update the record, if there is no record, a null exception will be reported

 

If you don't know whether to use the save() method or the update() method, you can use saveOrUpdate(), merge(), but after the merge(), it is in a disconnected state

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326845457&siteId=291194637