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());

这两种方式都能获取一个主键自增长对象的持久化后的主键

posted @ 2013-11-28 20:30  无忧之路  阅读(1714)  评论(0编辑  收藏  举报
无忧之路