你无须去获取。Session.save()的返回值是一个Serializable接口,这个接口其实就是自动生成的主键id。比如,如果你的主键的类型是Long,那么可以这样:Long id = (Long) session.save(a);

解决方案 »

  1.   

    我个人觉得你在调用一个api之前,应该好好看看api的注释。
    下面是Hibernate的Session对象的save()方法的注释。你看看他的“@return the generated identifier”,是不是很明显? /**
     * Persist the given transient instance, first assigning a generated identifier. (Or
     * using the current value of the identifier property if the <tt>assigned</tt>
     * generator is used.) This operation cascades to associated instances if the
     * association is mapped with <tt>cascade="save-update"</tt>.
     *
     * @param object a transient instance of a persistent class
     * @return the generated identifier
     * @throws HibernateException
     */
    public Serializable save(Object object) throws HibernateException;