使用 hibernate + spring mvc 获取自动生成的密钥,保存时 [英] get auto generated key, on save, using hibernate + spring mvc

查看:29
本文介绍了使用 hibernate + spring mvc 获取自动生成的密钥,保存时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring MVC + Hibernate

@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;

// save
public <T> int save(T entity) throws DataAccessException {
    Session session = sessionFactory.getCurrentSession();
    session.save(entity);

}

作为新记录保存,新主键生成,自动递增(db.MySQL).我想根据上述方法获取并返回新的自动递增值.

As New Record Save , New Primary Key generated which in auto increment (db.MySQL). I want to get and return the new auto incremented value with respect to the above method.

更新我!

推荐答案

Save 方法应该返回生成的 ID:

Save method should return generated ID:

http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Session.html#save(java.lang.Object)

这篇关于使用 hibernate + spring mvc 获取自动生成的密钥,保存时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆