三种状态及相互转化
瞬时状态(transient):刚new出来一个对象,还没有被保存到数据库中
持久化状态(persistent):已经被保存到数据库中或刚从数据库中取得
离线状态(detached):数据库中有,但是session中不存在该对象
方法的代码实例(下面代码1-8是连贯的,但由于需要分条说明,因此拆开)
(1) save():把一个对象从[瞬时状态]转换成[持久化状态](要注意DB里没有ID重复的该条数据)。
SessionFactory sessionFactory = SessionFactoryBuilder.getInstance(); Session session = sessionFactory.openSession(); session.beginTransaction(); TestUser user1 = new TestUser("1", "name1"); // user变成瞬时状态 session.save(user1); // user变成持久化状态 session.getTransaction().commit(); // 发出insert语句 session.close(); // [输出LOG] // Hibernate: insert into test_user (user_name, user_id) values (?, ?)
(2) update():把一个对象从[瞬时状态]转换成[持久化状态](要注意DB里要有ID重复的该条数据)。
1 session = sessionFactory.openSession(); 2 session.beginTransaction(); 3 TestUser user2 = new TestUser("1", "name1-update(2)"); 4 session.update(user2); 5 session.getTransaction().commit(); 6 session.close(); 7 // [输出LOG] 8 // Hibernate: update test_user set user_name=? where user_id=?
(3) saveOrUpdate():当(1)(2)中不确定DB里是否有ID重复的该条数据时可以用saveOrUpdate()。
1 session = sessionFactory.openSession(); 2 session.beginTransaction(); 3 TestUser user3 = new TestUser("1", "name1-update(3)"); 4 TestUser user4 = new TestUser("2", "name2"); 5 session.saveOrUpdate(user3); 6 session.saveOrUpdate(user4); 7 session.getTransaction().commit(); 8 session.close(); 9 // [输出LOG] 10 // Hibernate: select testuser_.user_id, testuser_.user_name as user_nam2_0_ from test_user testuser_ where testuser_.user_id=? 11 // Hibernate: select testuser_.user_id, testuser_.user_name as user_nam2_0_ from test_user testuser_ where testuser_.user_id=? 12 // Hibernate: insert into test_user (user_name, user_id) values (?, ?) 13 // Hibernate: update test_user set user_name=? where user_id=?
(4) 一个commit内先save再多次update,commit时只会做一次insert和一次update。
1 session = sessionFactory.openSession(); 2 session.beginTransaction(); 3 TestUser user5 = new TestUser("3", "name3"); 4 session.save(user5); 5 user5.setUserName("name3-update(4)-1"); 6 session.update(user5); // 该条语句可省略 7 user5.setUserName("name3-update(4)-2"); 8 session.update(user5); // 该条语句可省略 9 user5.setUserName("name3-update(4)"); 10 // 在调用了save方法后,此时user已经是持久化对象了,被保存在了session缓存当中, 11 // 这时user又重新修改了属性值,那么在commit时,此时hibernate对象就会拿当前这个user对象和保存在session缓存中的user对象进行比较, 12 // 如果两个对象相同,则不会发送update语句,否则,如果两个对象不同,则会发出update语句。所以无论多少次update,最终只会发送一次update语句。 13 session.getTransaction().commit(); 14 session.close(); 15 // [输出LOG] 16 // Hibernate: insert into test_user (user_name, user_id) values (?, ?) 17 // Hibernate: update test_user set user_name=? where user_id=?
(5) get();load();list();uniqueResult();等查询语句,如果数据库中有该对象,则该对象也变成了[持久化状态],被session所托管。
1 session = sessionFactory.openSession(); 2 session.beginTransaction(); 3 TestUser user6 = session.get(TestUser.class, "1"); 4 TestUser user7 = session.load(TestUser.class, "2"); 5 @SuppressWarnings("unchecked") 6 List<TestUser> user8List = session.createQuery("from TestUser").list(); 7 user6.setUserName("name1-update(5)"); 8 user7.setUserName("name2-update(5)"); 9 user8List.get(2).setUserName("name3-update(5)"); 10 session.getTransaction().commit(); 11 session.close(); 12 // [输出LOG] 13 // Hibernate: select testuser0_.user_id as user_id1_0_0_, testuser0_.user_name as user_nam2_0_0_ from test_user testuser0_ where testuser0_.user_id=? 14 // Nov 17, 2017 3:14:20 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService 15 // INFO: HHH000397: Using ASTQueryTranslatorFactory 16 // Hibernate: select testuser0_.user_id as user_id1_0_, testuser0_.user_name as user_nam2_0_ from test_user testuser0_ 17 // Hibernate: update test_user set user_name=? where user_id=? 18 // Hibernate: update test_user set user_name=? where user_id=? 19 // Hibernate: update test_user set user_name=? where user_id=?
(6) evict();clear();close();将session的缓存对象清空evict()清除指定对象;clear()&close()清除所有对象。它们使对象从[持久化状态]变成[离线状态]。
1 session = sessionFactory.openSession(); 2 session.beginTransaction(); 3 TestUser user9 = session.get(TestUser.class, "1"); 4 TestUser user10 = session.get(TestUser.class, "2"); 5 user9.setUserName("name1-update(6)"); 6 user10.setUserName("name2-update(6)"); 7 session.evict(user9); 8 session.clear(); 9 session.getTransaction().commit(); 10 session.close(); 11 // [输出LOG] 12 // Hibernate: select testuser0_.user_id as user_id1_0_0_, testuser0_.user_name as user_nam2_0_0_ from test_user testuser0_ where testuser0_.user_id=? 13 // Hibernate: select testuser0_.user_id as user_id1_0_0_, testuser0_.user_name as user_nam2_0_0_ from test_user testuser0_ where testuser0_.user_id=?
(7) update();saveOrUpdate();使对象从[离线状态]转成[持久化状态]。
1 session = sessionFactory.openSession(); 2 session.beginTransaction(); 3 TestUser user11 = session.get(TestUser.class, "1"); // 持久化状态 4 user11.setUserName("name1-update(7)"); 5 session.evict(user11); // 离线状态 6 session.update(user11); // 持久化状态 7 session.getTransaction().commit(); 8 session.close(); 9 // [输出LOG] 10 // Hibernate: select testuser0_.user_id as user_id1_0_0_, testuser0_.user_name as user_nam2_0_0_ from test_user testuser0_ where testuser0_.user_id=? 11 // Hibernate: update test_user set user_name=? where user_id=?
(8) delete();从[瞬时状态]和[持久化状态]都可以删除。
1 session = sessionFactory.openSession(); 2 session.beginTransaction(); 3 TestUser user12 = new TestUser("1", "name1"); 4 session.delete(user12); 5 TestUser user13 = session.get(TestUser.class, "2"); 6 session.delete(user13); 7 session.getTransaction().commit(); 8 session.close(); 9 // [输出LOG] 10 // Hibernate: select testuser_.user_id, testuser_.user_name as user_nam2_0_ from test_user testuser_ where testuser_.user_id=? 11 // Hibernate: select testuser0_.user_id as user_id1_0_0_, testuser0_.user_name as user_nam2_0_0_ from test_user testuser0_ where testuser0_.user_id=? 12 // Hibernate: delete from test_user where user_id=? 13 // Hibernate: delete from test_user where user_id=?