欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

hibernate更新数据方法小结

程序员文章站 2023-12-05 15:46:22
复制代码 代码如下:usertable user=null; session session=hibernatesessionfactory.getsession(); s...
复制代码 代码如下:

usertable user=null;
session session=hibernatesessionfactory.getsession();
string sql="from usertable as user where user.username=?";
query q=session.createquery(sql);
q.setstring(0,username);
list l=q.list();
iterator ite=l.iterator();
if(ite.hasnext()){
user=(usertable)ite.next();
}
return user;

查询完以后才能更新用session.update就行了。
hibernate的session接口的update()方法可以更新持久化对象.使其对象属性的状态改变传递到数据库.

复制代码 代码如下:

public boolean updatenews(devinfo df) throws hibernateexception{
net.sf.hibernate.session sess =hibernateutil.currentsession();
transaction tx = sess.begintransaction();
try{
devinfo d=new devinfo();
sess.load(d,df.getnewsid());
d.settypeid(df.gettypeid());
d.setsubject(df.getsubject());
d.setcontent(df.getcontent());
d.setmdate(new date((new java.util.date()).gettime()).tostring());
sess.update(d);
tx.commit();
hibernateutil.closesession();
return true;
}
catch(hibernateexception e){
e.printstacktrace();
return false;
}
}