文章詳情頁
Hibernate 修改數(shù)據(jù)的實(shí)例詳解
瀏覽:221日期:2022-06-07 10:44:31
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();
- 使用 session.update()方法,根據(jù)主鍵去更新數(shù)據(jù),如果數(shù)據(jù)存在,那么就可以更新,如果不存在,拋異常報(bào)錯
- 可以使用 session.saveOrUpdate(person);方法,根據(jù)主鍵去更新數(shù)據(jù),如果數(shù)據(jù)存在,那么就可以更新,如果不存在,就執(zhí)行 insert
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
標(biāo)簽:
JSP
相關(guān)文章:
1. Hibernate 主清單文件配制的詳細(xì)介紹2. springboot2.3之后hibernate-validator依賴缺失【踩坑】3. vue element實(shí)現(xiàn)表格增加刪除修改數(shù)據(jù)4. Springboot hibernate envers使用過程詳解5. IDEA2019.3配置Hibernate的詳細(xì)教程(未使用IDEA的自動化)6. java中Hibernate緩存形式總結(jié)7. Idea+maven搭建SSH(struts2+hibernate5+spring5)環(huán)境的方法步驟8. java中Hibernate的狀態(tài)總結(jié)9. Hibernate傳入Java對象創(chuàng)建動態(tài)表并錄入數(shù)據(jù)10. Java web Hibernate如何與數(shù)據(jù)庫鏈接
排行榜

網(wǎng)公網(wǎng)安備