How can I prevent Hibernate from updating NULL values

前端 未结 3 838
执笔经年
执笔经年 2021-02-01 06:10

Is there a setting in hibernate to ignore null values of properties when saving a hibernate object?

NOTE
In my case I am de-serial

3条回答
  •  情书的邮戳
    2021-02-01 06:30

    I have googled a lot about this,but there is no the very solution for me.So,I used a not graceful solution to cover it.

      public void setAccount(Account a) throws HibernateException {
        try {
            Account tmp = (Account) session.
                    get(Account.class, a.getAccountId());
            tmp.setEmail(getNotNull(a.getEmail(), tmp.getEmail()));            
            ...
            tmp.setVersion(getNotNull(a.getVersion(), tmp.getVersion()));
            session.beginTransaction();
            session.update(tmp);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            logger.error(e.toString());
            throw e;
        }
    }
    
    public static  T getNotNull(T a, T b) {
        return b != null && a != null && !a.equals(b) ? a : b;
    }
    

    I receive an Object a which contains a lot of fields.Those field maybe null,but I don't want to update them into mysql. I get an tmp Obejct from db, and change the field by method getNotNull,then update the Object.

    a Chinese description edition

提交回复
热议问题