Hibernate save或者persist 后获取主键ID
一个自增长ID的对象被save或者persist后,会返回其主键ID:
Department department = new Department(); department.setName("xxxxxxx"); session.persist(department); System.out.println("id = " + department2.getId());
Department department2 = new Department(); department2.setName("xxxxxxx"); session2.save(department2); System.out.println("id2 = " + department2.getId());
这两种方式都能获取一个主键自增长对象的持久化后的主键
本文出自 无忧之路 - 博客园