Hibernate 修改數(shù)據(jù)
1.用 HQL方式來更新
在 這里修改 Person 的name 和age 通過 id 標(biāo)識
Session currentSession = H3Utils.getCurrentSession(); currentSession.beginTransaction(); //創(chuàng)建 HQL String hqlString = "update Person p set p.name=? , p.age=? where p.id=?"; //構(gòu)建 Query Query query = currentSession.createQuery(hqlString); //設(shè)置參數(shù) query.setParameter(0, "小明"); query.setParameter(1, 18); query.setParameter(2, 1); //更新 query.executeUpdate(); currentSession.getTransaction().commit();
2 使用 HQL方式來更新
public void updateFunction2() { Session currentSession = H3Utils.getCurrentSession(); currentSession.beginTransaction(); //創(chuàng)建SQL String sql = "UPDATE t_person_list SET name='cv',age=2 WHERE id=4" ; //執(zhí)行 currentSession.createSQLQuery(sql).executeUpdate(); //提交 currentSession.getTransaction().commit(); }
3 使用 OID方式來更新
Session currentSession = H3Utils.getCurrentSession(); currentSession.beginTransaction(); Person person = new Person(); person.setId(44); person.setName("ccb"); person.setAge(90); currentSession.update(person); currentSession.getTransaction().commit();
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
標(biāo)簽:南京 黃石 廣州 樂山 貸款邀約 銅川 大連 內(nèi)江
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Hibernate 修改數(shù)據(jù)的實(shí)例詳解》,本文關(guān)鍵詞 Hibernate,修改,數(shù)據(jù),的,實(shí)例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。