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(); }
==============控制台信息
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]true
===============》第二次没有向数据库查询数据。两个对象相同