Hibernate更新数据
来源:互联网 发布:上海青少年编程培训 编辑:程序博客网 时间:2024/05/21 19:13
public boolean update(Livestock ls) throws HibernateException {
Transaction tx = null;
try {
Session session = this.getSession();
tx = session.beginTransaction();
Livestock d = new Livestock();
session.clear();//此方法不调用有可能出错,a different object with the same identifier value was already associated with the session:
session.load(d, ls.getEarTag());
d.setType(ls.getType());
d.setWeight(ls.getWeight());
d.setSupplyBill(ls.getSupplyBill());
d.setWareStatus(ls.getWareStatus());
d.setCarcasses(ls.getCarcasses());
session.update(d);
session.flush();
tx.commit();
return true;
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null)
tx.rollback();
return false;
} finally {
this.sessionClose();
}
Transaction tx = null;
try {
Session session = this.getSession();
tx = session.beginTransaction();
Livestock d = new Livestock();
session.clear();//此方法不调用有可能出错,a different object with the same identifier value was already associated with the session:
session.load(d, ls.getEarTag());
d.setType(ls.getType());
d.setWeight(ls.getWeight());
d.setSupplyBill(ls.getSupplyBill());
d.setWareStatus(ls.getWareStatus());
d.setCarcasses(ls.getCarcasses());
session.update(d);
session.flush();
tx.commit();
return true;
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null)
tx.rollback();
return false;
} finally {
this.sessionClose();
}
}
Session.load执行过程:
在执行session.load时,Hibernate首先从当前session的一级缓存中获取id对应的值,如果获取不到,将根据该对象是否配置了二级缓存来做相应的处理,若配置了二级缓存,则从二级缓存中获取id对应的值,若仍然获取不到则还需要根据是否配置了延迟加载来决定如何执行,若未配置延迟加载则从数据库中直接获取,在从数据库获取到数据的情况下,Hibernate会相应的填充一级缓存和二级缓存,如配置了延迟加载则直接返回一个代理类,只有在触发代理类的调用时才进行数据库查询的操作。
一级缓存----->二级缓存------>延迟加载-------->数据库
因此在session一直打开的情况下,要注意在适当的时候对一级缓存进行刷新操作,通常是在该对象具有单向关联维护的时候,在Hibernate中可以使用象session.clear、session.evict的方式来强制刷新一级缓存。
二级缓存则在数据发生任何变化(新增、更新、删除)的情况下都会自动的被更新。
- hibernate更新数据问题
- hibernate更新数据
- Hibernate批量更新数据
- Hibernate更新数据
- Hibernate更新数据
- Hibernate 批量更新数据
- hibernate 更新数据注意事项
- Hibernate数据更新(九)
- hibernate手动更新数据 查询数据与更新数据不同步
- Hibernate入门07 - 更新,删除数据
- Hibernate入门07 - 更新,删除数据
- Hibernate 数据的保存,更新和删除
- Hibernate 数据的保存,更新和删除
- Hibernate入门之更新,删除数据
- hibernate 没有id属性 怎么更新数据
- hibernate数据修改后不能及时更新
- hibernate批量删除和更新数据
- Hibernate中更新数据的update方法
- 清除所有cookie
- ubuntu下用Eclipse搭建C++开发环境
- tx面试
- SSH整合,spring事务管理不回滚问题
- FastDFS和集中存储方式对比
- Hibernate更新数据
- “Microsoft Visual C++ Runtime Library”错误
- Eclipse重要快捷键
- 原来我们是DevKit8000
- Linux 客户端与服务器SSH连接不会断掉
- oracle ORA-00844: Parameter not taking MEMORY_TARGET into account, see alert log for mo
- cvLoadImage 不能将参数1从 "_TCHAR*" 转换为"const char *"
- Linux上的free命令详解
- FTP协议命令+返回值+返回值解析