What is the fastest way to get the persisted object after calling Hibernate's saveOrUpdate?

Posted by Dave on Stack Overflow See other posts from Stack Overflow or by Dave
Published on 2012-09-24T21:36:25Z Indexed on 2012/09/24 21:37 UTC
Read the original article Hit count: 177

Filed under:
|

I'm using Hibernate 3.2.1.ga, hibernate annotations 3.2.1.ga, and hibernate-jpa-2.0-api. I can't upgrade at this time as I'm working with legacy code. I have this generic method for saving or updating objects ...

protected void saveOrUpdate(Object obj) {
    final Session session = sessionFactory.getCurrentSession();
    session.saveOrUpdate(obj);
}

You can assume that every argument, "obj," will have a member field that is marked with the "@Id" annotation. I would like to change the return type to return an Object that represents the persisted object in the database (meaning if "obj" didn't contain an id before, what is returned is the database object with a populated id. What is the fastest way to do this given my versioning and generic constraints?

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about save