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
===============》第二次没有向数据库查询数据。两个对象相同
当有符合主键时,一方与多方的复合主键顺序必须一致:
<set>
<key>
<column name="A" />
<column name="B" />
<key>
<one-to-many class="" />
</set>
<many-to-one name="" class="">
<column name="A" />
<column name="B" />
<many-to-one/>