2

我想做这个 :

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

但我必须这样做:

@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);
    }

那正确吗 ?那似乎很蹩脚。

4

1 回答 1

3

你可以这样做return myObject。Hibernate 将标识符与作为参数给定的对象相关联save

于 2012-07-25T16:47:37.670 回答