NimChimpsky
NimChimpsky

Reputation: 47280

session.save does not return the persisted object, only the identifier

I want to do this :

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public MyObject create(ObjectBasicDTO dto) {
    MyObject myObject = new MyObject (dto);
    return sessionFactory.getCurrentSession().save(object);
    }

But I have to do this :

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public MyObject create(ObjectBasicDTO dto) {
    MyObject myObject = new MyObject (dto);
    Long id = sessionFactory.getCurrentSession().save(object);
    return sessionFactory.getCurrentSession().get(MyObject.class, id);
    }

Is that right ? Thats seems lame.

Upvotes: 2

Views: 3433

Answers (1)

tibtof
tibtof

Reputation: 7957

You could do return myObject instead. Hibernate associates the identifier to the object given as parameter to save .

Upvotes: 3

Related Questions