Hibernate Session.delete() an object if exists

前端 未结 7 1774
谎友^
谎友^ 2020-12-31 03:41

In JavaDoc of Session class the description of delete method is:

Remove a persistent instance from the datastore. The argument may be

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-31 04:18

    Try this...

    public  T delete(T t) throws Exception {
        try {
            t = load(t);
            session.delete(t);
            session.flush();
        } catch (Exception e) {
            throw e;
        } finally {
            session.clear();
        }
    
        return t;
    }
    
    public  T load(T t) {
        session.buildLockRequest(LockOptions.NONE).lock(t);
        return t;
    }
    

提交回复
热议问题