随笔-3  评论-2  文章-0  trackbacks-0
  2008年9月23日

session.update不能直接用,因为他是以ID为主键,必须查询出ID才能进行更新。。。

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就行了。

posted @ 2008-09-23 20:12 想想 阅读(243) | 评论 (0)编辑 收藏
请高手教教我吧.
在hibernate中查询数据,查询语句中包含sql函数String sql = (select * from employee emp where left(dbo.f_GetPy(name,1)='要查询的字母')
其中dbo.f_GetPy(name),1)为sql server 2000中的自定义函数,"name"是列名。其作用是按汉字的字符串的第一个字的英文首字母查出相应的字符串(按首字母查询)。
我用的是List list = session.createSQLQuery(sql)
.addEntity("emp",Employee.class)
.list() ;
方法。
报的错误是.list()那行。sql语句在查询分析器中试过了,没问题。具体错误代码如下:

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: could not execute query
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
yfy.struts.MyActionServlet.process(MyActionServlet.java:17)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

org.hibernate.exception.SQLGrammarException: could not execute query
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.loader.Loader.doList(Loader.java:2148)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
org.hibernate.loader.Loader.list(Loader.java:2024)
org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:111)
org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1655)
org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:164)
yfy.dao.BaseDaoImpl.execSQLQuery(BaseDaoImpl.java:138)
yfy.struts.action.EmpNameListAction.execute(EmpNameListAction.java:40)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
yfy.struts.MyActionServlet.process(MyActionServlet.java:17)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

请高手们帮我解答。谢谢
posted @ 2008-09-23 19:01 想想 阅读(499) | 评论 (2)编辑 收藏
仅列出标题