public void saveUser(){
Session session=JdbcUtil.getSession();
User user=new User();
user.setName("zjf2");
user.setAddress("gzy2");
Transaction tx=session.beginTransaction();
user.setAge(23);
user.setEmploy(1002);
session.save(user);
System.out.println(user);
tx.commit();
User user2=(User) session.get(User.class, user.getId());
System.out.println(user2);
System.out.println(user==user2);
session.close();
}數據庫
==============控制檯信息session
Hibernate: select max(id) from user
User [id=22, name=zjf2, age=23, address=gzy2, employ=1002]
Hibernate: insert into user (name, age, address, employ, id) values (?, ?, ?, ?, ?)
User [id=22, name=zjf2, age=23, address=gzy2, employ=1002]
truespa
===============》第二次沒有向數據庫查詢數據。兩個對象相同對象