如例: package util; import org.hibernate.Session; import org.hibernate.Transaction; import pojo.User; public class Manager { public static void main(String[] args) { User user = new User(); user.setAge(18); user.setName("ggg"); user.setPassword("666"); //瞬时状态 System.out.println("瞬时状态时,对象ID:" + user.getId()); Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction = session.beginTransaction(); session.save(user); //持久状态 System.out.println("持久状态时,对象ID:" + user.getId()); transaction.commit(); session.close(); //脱管状态 System.out.println("脱管状态时,对象ID:" + user.getId()); } } 输出日志: 瞬时状态时,对象ID:null 持久状态时,对象ID:402881792b69e6df012b69e6e0eb0001 Hibernate: insert into A_USER (NAME, PASSWORD, AGE, ID) values (?, ?, ?, ?) 脱管状态时,对象ID:402881792b69e6df012b69e6e0eb0001