您的位置:程序门 -> java -> 框架、开源



求 hibernate 取 最大 id 如何写。。。我写的出错晕


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求 hibernate 取 最大 id 如何写。。。我写的出错晕
发表于:2008-01-16 23:15:20 楼主
public   static   int   getmaxfileid(string   sql)   {
int   id   =   0;
transaction   transaction   =   null;
session   session   =   null;
try   {
session   =   hibernateservice.getsession();
transaction   =   session.begintransaction();
id   =   integer.parseint(session.createquery(sql).tostring());
transaction.commit();
}   catch   (hibernateexception   he)   {
he.printstacktrace();
hibernateservice.rollbacktransaction(transaction);
id   =   0;
}   catch   (exception   e)   {
e.printstacktrace();
id   =   0;
}   finally   {
hibernateservice.closesession(session);
}
return   id;
}


传入是

int   fileid   =   fileutil.getmaxfileid("select   max(id)   from   file")+1;


该如何修改啊?
发表于:2008-01-17 06:18:121楼 得分:0
java code
id = integer.parseint(session.createquery(sql).list().get(0).tostring());
发表于:2008-01-17 09:15:262楼 得分:0
只能用sql
发表于:2008-01-17 10:16:083楼 得分:0
参考下:
if   (dto.getcategoryid()   ==   null)   {
string   categoryidmax   =   whbzser.findmaxcategoryid(rc.getdbsession());
                long   l_categoryid   =   long.parselong(categoryidmax)   +   1;
dto.setcategoryid(new   long(l_categoryid));
}
//-dao里面的方法
public   static   string   findmaxcategoryid(session   session)   throws   exception   {
stringbuffer   hql   =   new   stringbuffer();
hql.append("select   max(t.categoryid)   from   texpcategory   t");
string   maxid   =   "0";

list   list   =   privilegedao.querylistbypage(session,   hql.tostring());
if   (list   !=   null   &&   list.size()   >   0)   {
maxid   =   list.get(0).tostring();
}

return   maxid;
}    


快速检索

最新资讯
热门点击