1. 程式人生 > >關於更新部分屬性,Hibernate更新某些欄位的幾種update方法

關於更新部分屬性,Hibernate更新某些欄位的幾種update方法

 1.在hbm.xml中設定property 標籤 update = “false” 例如

    <property name=”age”update=”false”></property>

    我們在執行 Update方法會發現,age 屬性 不會被更改,缺點:不靈活。

    Hibernate:

        UPDATE

        Teacher

        SET

        birthday=?,

        name=?,

        title=?

        WHERE

        id=?

2. 使用hbm.xml中的 dynamic-update="true"

<classname="com.sccin.entity.Student" table="student"dynamic-update="true"/>

    3.使用HQL語句(靈活,方便),使用HQL語句修改資料.

Query query =session.createQuery("update A a set a.name = :name where id         = :id");

    4. 將需要更新的物件載入上來後,利用

        org.springframework.beans.BeanUtils類的 copyProperties方法,將需要         更新的值copy到物件中,最後再呼叫update方法。

由於本系統使用1和2方法,並未成功,所以採用4方法。

        Useru = findUserById(user.getUid());

    BeanUtils.copyProperties(user,u,newString[]{"uid","username","password","cr    eatetime"});

    getSession().update(u);