大家好,hibernate不是可以只更新部分数据,而其它的保留不变吗,我是这样做的,但是其它不需要更新的字段的数据却没有 类 Ci 它有五个属性 id, name, displayName,discription,createTime 原来都是有数据的,而我 Ci ci = new Ci();
ci.setId(1);
ci.setDisplayName("报表");
getHibernateTemplate().saveOrUpdate(ci); 数据表只有displayName字段的数据,而其它字段全都没了,是什么原因啊
ci.setId(1);
ci.setDisplayName("报表");
getHibernateTemplate().saveOrUpdate(ci); 数据表只有displayName字段的数据,而其它字段全都没了,是什么原因啊
解决方案 »
- 大数据量导出到Csv
- 在login控件里面添加的textbox1怎么读取里面的数值啊
- 请教编译问题:API的更新
- Servlet和JavaBean
- jsp 分页并排序
- 为什么把msbase.jar等3个包放到WEB-INF\lib中出现Cannot load JDBC driver class错误,而放到Tomcat的common\lib就没有问题呢?
- 各位大虾,小弟刚学Java不久,不知道为什么写的jsp程序调用class的方法没有反应???请多多指教。。。
- 小问题,简单问题,送分问题!快来抢啊!
- 如何把一个表单的内容以邮件的形式发送出去!
- 根本解决Tomcat中的中文问题!
- Hibernate的c3p0连接池问题
- 初学struts的问题,在线等,求高人帮忙
更新的话应该先getHibernateTemplate().load(Ci.class,1),然后再set,最后再update
public void update(Long ciid,Long querytype, String createby, String displayname, String description){
IaSysQuery iaSysQuery = (IaSysQuery)this.getHibernateTemplate().load(IaSysQuery.class, ciid);
Ci ci = (Ci)this.getHibernateTemplate().load(Ci.class, ciid);
iaSysQuery.setQuerytype(querytype);
iaSysQuery.setCreateby(createby);
ci.setDisplayname(displayname);
ci.setDescription(description);
getHibernateTemplate().saveOrUpdate(iaSysQuery);
getHibernateTemplate().saveOrUpdate(ci);
} 但是我这样做却有中文乱码问题这是为什么呢 public void updateIaSysQuery(Long querytype, String createby, Long ciid) {
Session session = super.getSession();
Query query = session.createQuery("update IaSysQuery set querytype=? , createby=? where ciid=?");
query.setLong(0, querytype);
query.setString(1, createby);
query.setLong(2, ciid);
query.executeUpdate();
}
public void updateCi(String displayname, String description, Long ciid) {
Session session = super.getSession();
Query query = session.createQuery("update Ci set displayname=? , description=? where id=?");
query.setString(0, displayname);
query.setString(1, description);
query.setLong(2, ciid);
query.executeUpdate();
}
而对于修改数据来说,那是修改已存在对象的状态
而你的操作:
Ci ci = new Ci();
ci.setId(1);
ci.setDisplayName("报表");
getHibernateTemplate().saveOrUpdate(ci);
实际上调用的是save方法,只是这个save很特殊
他相当于如下代码:
A a = new A();
假设A对象有很多属性
而我们只调用了a对象的某个set方法
所以其他属性的值是null
而saveOrUpdate方法是如果没有就执行save
有就执行update
你这里是有
只是所有属性都是null而已
而你所设的id呢又恰好和数据库中的某个id相同
所以执行了update方法
其他值hibernate主动给你调用了set(null);
所以就全部是null