1、使用对象更新

	public void updateImagePath(Weibo weibo){
Session session = HibernateUtil.currentSession();
try {
Transaction tx = session.beginTransaction();
session.update(weibo);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
System.err.println("search failed!!!!!!");
}finally{
HibernateUtil.closeSession();
HibernateUtil.sessionFactory.close();
}
}

2、使用sql语句更新

	public int updateImagePath1(String imagePath, String weiboId){
Session session = HibernateUtil.currentSession();
try {
String sql = "update t_weiboo set imagePath='"+imagePath+"' where weiboId='M_Boj1Wc4xC';";
Transaction tx = session.beginTransaction();
Query query = session.createSQLQuery(sql);
int state= query.executeUpdate();
tx.commit();
if(state==1)
System.out.println("修改路径成功!");
return state;
} catch (Exception e) {
// TODO: handle exception
System.err.println("search failed!!!!!!");
}finally{
HibernateUtil.closeSession();
HibernateUtil.sessionFactory.close();
}
return 0;
}

3、先通过id获取对象,再更新

	public void update1() {

		Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Weibo wb = (Weibo) session.get(Weibo.class, 3);
wb.setImagePath("");
session.update(wb);
tx.commit(); }

hibernate更新的更多相关文章

  1. 使用hibernate更新数据库记录的信息的相关学习记录

    截选代码(可能遗漏标点符号): package name.sql; import java.util.List; import name.session.HibernateSessionFactory ...

  2. Hibernate更新数据报错:a different object with the same identifier value was already associated with the session: [com.elec.domain.ElecCommonMsg#297e35035c28c368015c28c3e6780001]

    使用hibernate更新数据时,报错 Struts has detected an unhandled exception: Messages: a different object with th ...

  3. Hibernate 更新部分字段的实现

    在Hibernate 中,有时我们只需要更新部分字段,此时如果使用update()方法,会将所有字段都更新,对于没有set的字段,就会设置成NULL,如果这些字段里面有非空的字段就会报错. 解决的方法 ...

  4. Hibernate更新部分字段浅谈

    update语句是在Hibernate的Configuration的时候生成的,不能动态改变.为什么update的时候所有的属性都一起update,而不是只更新改变字段,其实这是一个比较值得探讨的问题 ...

  5. Hibernate更新数据(不用update也可以)

    在介绍hibernate的更新之前,我们先来看看session的两个方法.load和get方法:这两个方法是获取数据的根据对象的id值: 先看两段代码.load和get的方法都含有两个参数,前者是得到 ...

  6. Hibernate更新删除数据后,再查询数据依然存在的解决办法

    删除数据后,重新查询了数据库,DB中记录已经删除了,但是数据依然能查询到,网上都说是Hibernate的缓冲问题. 我对session进行了clear,flush,并且在事务和查询中都对session ...

  7. [原创]hibernate更新后jdbc读取不到数据问题

    最近在做工作流插件时使用的是自己基于hibernate连接封装的orm框架,按说跟hibernate共用的一个连接,应该在同一个事务中,但在使用时hibernate saveOrUpdate后(未提交 ...

  8. Hibernate更新某些字段的几种update方法

    Hibernate 中如果直接使用 Session.update(Object o); 会把这个表中的所有字段更新一遍. 比如: public class TeacherTest { @Test pu ...

  9. Hibernate更新、删除后数据库无变化

    转自:https://ask.csdn.net/questions/756109 !-- 配置事务管理器 --> <tx:advice id="advice" tran ...

随机推荐

  1. vue项目报错1 Vue is a constructor and should be called with the `new` keyword && jquery.js?eedf:3850 Uncaught TypeError: this._init is not a function...

    Vue is a constructor and should be called with the `new` keyword Uncaught TypeError: this._init is n ...

  2. tar/gzip/zip文件打包、压缩命令

    一.tar打包备份工具 1.命令功能 tar 将多个文件或目录打包在一起,可用通过调用gzip或zip实现压缩.解压的命令:tar不仅可以多多个文件进行打包,还可以对多个文件打包后进行压缩. 2.语法 ...

  3. Codeforces 898 贪心关闭最少闹钟 优先队列最少操作构造N/2squares 讨论情况哈希数字串分割a+b=c

    A /* Huyyt */ #include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define mkp(a,b) ...

  4. H5手机端开发问题汇总及解决方案

    前言 少侠,请留步,相见必是缘分,赠与你一部<踏坑秘籍> 扎马步 踏坑第一式 ios竖屏拍照上传,图片被旋转问题 解决方案 // 几个步骤 // 1.通过第三方插件exif-js获取到图片 ...

  5. C语言字符串追加,双色球等案例

    //C语言中没有字符串概念,有的只是字符型数组,以str1[]的值为例,该字符数组的长度为11--->包含了字母,空格,以及结束字符'\0'(斜杠0)//基于上述原因,读取一个字符型数组的有效方 ...

  6. NOIP2016提高A组模拟中秋节9.15总结

    这套题不算难但是比赛上萎掉了. 第一题数论, 当找到一个合适的数就直接处理答案,再用筛法将处理过的删掉. 比赛上没想到筛法,只拿了70分. 第二题二分答案,然后验证合法性就可以. 但是由于不能二分小数 ...

  7. 获取树莓派ip地址的方法

    1.有显示器​ (1)鼠标停留在屏幕右上角网络图标上,2-3秒会显示网络连接信息​(2) 图形界面下打开终端运行ifconfig命令 2.登录路由器​查看名叫raspberry的设备 3.电脑上运行命 ...

  8. spring boot2.0.2,<-1.4.8

    DataSourceBuilder cannot be resolved DataSourceBuilder cannot be resolved to a type RelaxedPropertyR ...

  9. k8s-dashboard搭建

    一,简单搭建,未使用ssl证书,可载谷歌浏览器访问 1,拉取镜像 docker pull gcrxio/kubernetes-dashboard-amd64:v1.10.1 docker tag gc ...

  10. linux-awk-3

    awk 基础语法 Awk –Fs '/pattern/ {action}' input-file (或者) Awk –Fs '{action}' input-file -F 为字段分界符.如果不指定, ...