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. springboot中的编码设置

    在springboot中编码配置可以通过filter也可以通过springboot的核心配置文件application.properties中配置如下信息: #配置字符编码spring.http.en ...

  2. vue.js(3)--v-bind与v-on

    vue中的v-bind与v-on的使用 (1)实例 <!DOCTYPE html> <html lang="en"> <head> <me ...

  3. 68. Text Justification (JAVA)

    Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...

  4. Ubuntu换阿里云源

    sudo -s cd /etc/apt gedit source.list deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted u ...

  5. 013-zabbix trapper方式监控

    zabbix获取数据时有时会出现超时,如果一些数据需要执行比较长的时间才能获取的话,那么zabbix会出现异常,考虑到这种情况,zabbix增加了Trapper功能,客户端自己提交数据给zabbix. ...

  6. PAT Basic 1043 输出PATest (20 分)

    给定一个长度不超过 ​ ​​ 的.仅由英文字母构成的字符串.请将字符重新调整顺序,按 PATestPATest.... 这样的顺序输出,并忽略其它字符.当然,六种字符的个数不一定是一样多的,若某种字符 ...

  7. java面试(进程和线程)04

    1.并行和并发有什么区别? 并行:多个处理器或多核处理器同时处理多个任务. 并发:多个任务在同一个 CPU 核上,按细分的时间片轮流(交替)执行,从逻辑上来看那些任务是同时执行. 2.线程和进程的区别 ...

  8. git学习补充

    关系图 git checkout -- target 放弃 cached 中 对 target 文件内容已作的修改 git checkout . 放弃当前目录下对于 cached 的所有修改. 对比: ...

  9. c语言获取系统时间并格式化

    // #include <time.h> int GetAndFormatSystemTime(char* timeBuff) { if (timeBuff == NULL) { retu ...

  10. 【NOIP2016提高A组五校联考1】挖金矿

    题目 分析 我们二分答案 设\(sum_{i,j}\)表示的i列前个数的和, 假设当前出的二分答案为x,第i列挖了\(h_j\)层,则 \[\dfrac{\sum_{i=1}^{n}sum_{i,h_ ...