Hibernate 修改数据
1.用 HQL方式来更新
在 这里修改 Person 的name 和age 通过 id 标识
Session currentSession = H3Utils.getCurrentSession();currentSession.beginTransaction();//创建 HQL String hqlString = "update Person p set p.name=? , p.age=? where p.id=?";//构建 Query Query query = currentSession.createQuery(hqlString);//设置参数 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();//创建SQL String sql = "UPDATE t_person_list SET name='cv',age=2 WHERE id=4" ;//执行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()方法,根据主键去更新数据,如果数据存在,那么就可以更新,如果不存在,抛异常报错可以使用 session.saveOrUpdate(person);方法,根据主键去更新数据,如果数据存在,那么就可以更新,如果不存在,就执行 insert
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载像素火影骨架佐助 (Perseverance Fire Shadow)手机版v1.16
下载要塞英雄 安卓版v33.20.0-39082670-Android
下载梦想城镇vivo最新版本 安卓版v12.0.1
梦想城镇vivo版是这款卡通风模拟经营类手游的渠道服版本,玩
怦然心动的瞬间 安卓版v1.0
怦然心动的瞬间是一款真人向的恋爱互动游戏,在游戏中玩家将扮演
曼尼汉堡店游戏 安卓版v1.0.3
曼尼汉堡店是一款非常好玩的精品恐怖类型冒险游戏,在这款游戏中
现代总统模拟器去广告版 安卓版v1.0.46
现代总统模拟器是一款休闲养成类游戏,可能对于不少的玩家来说都
现代总统模拟器付费完整版 安卓版v1.0.46
现代总统模拟器高级版在商店是需要付费的,相对于普通版本,高级