日期:2014-05-20  浏览次数:20802 次

hibernate异常:addEntity() or addScalar() must be called on a sql query before executing the query.
public Generator() {
  }


  /**
  *
  * @param id
  * @return
  * @throws Exception
  */
  public static synchronized String getLineNumber(int id)throws Exception { //
  String sql ="select number_cur_value from number_factory where number_id='" + id+"'"; // number_isRead ='0' and 
  int ii = 0;
  try {
  Session session = HibernateUtil.currentSession();

  HibernateUtil.beginTransaction();

  SQLQuery querey = session.createSQLQuery(sql);

  List list1 = querey.list();
  if(list1!=null && list1.size()>0){
  String val = (String)list1.get(0);  
 
  if(val!=null && !val.trim().equals("")){
  ii = Integer.valueOf(val)+1;

  String sql2="update number_factory set number_cur_value="+ii+" where number_id="+id;//, number_isRead='0'
  SQLQuery exeUpdate = session.createSQLQuery(sql2);
  exeUpdate.executeUpdate();
  }
  }
  HibernateUtil.commitTransaction();
  } catch (HibernateException e) {
  throw e;
  } finally {
  HibernateUtil.closeSession();
  }

  return ii+"";
  }
}


------解决方案--------------------
ddddddddddddddddddddddddddd 

遇到了同样的问题, 
若根据提示 在后面加 addEntity(),又有新的异常 : 
javax.servlet.ServletException:could not execute query 
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545) 
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486) 
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274) 
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482) 
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:709) 

汗! 解决中………… 
期待那位大虾帮忙解决一下啊! 
楼主,一起等吧!