Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get auto generated key, on save, using hibernate + spring mvc

I am using 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);

}

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.

Update me !

like image 916
Shahid Ghafoor Avatar asked Mar 23 '23 17:03

Shahid Ghafoor


1 Answers

Try this instead. This works with latest Hibernate (version 4.1) also.

session.persist(object);
object.getId();
like image 178
Ravi Chhatrala Avatar answered Apr 02 '23 07:04

Ravi Chhatrala