Hibernate + Servlet的一个奇怪的问题!搞了一天了。。烦啊。
*********************************************普通类
package com.gdc8888.implement;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Transaction;
import com.gdc8888.hibernate.DAOBase;
import com.gdc8888.hibernate.HibernateSessionFactory;
import com.gdc8888.interfaces.ITblPCardType;
import com.gdc8888.interfaces.ITblPointCards;
import com.gdc8888.util.StringUtil;
import com.gdc8888.vo.TblPCardTypeVO;
import com.gdc8888.vo.TblPointCardsVO;
public class TblPointCardsImpl extends DAOBase implements ITblPointCards {
private String newID="";
public boolean addPointCard(TblPointCardsVO vo) {
try {
Transaction tx=session.beginTransaction();
session.save(vo);
String radStr = vo.getPocaid() + StringUtil.randomString();
// vo.setPocaid(radStr);
// session.update(vo);
String sql="update TblPointCardsVO as vo set vo.pocaid=? where vo.pocaid=?";
Query query = session.createQuery(sql);
query.setString(0, radStr);
query.setString(1, vo.getPocaid());
query.executeUpdate();
//
tx.commit();
newID=radStr;
} catch (Exception e) {
System.out.println("添加卡的类型时出错");
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args) {
TblPointCardsVO vo = new TblPointCardsVO();
ITblPointCards dao = new TblPointCardsImpl();
TblPCardTypeVO tvo = new TblPCardTypeVO();
tvo.setPcardmoney("100");
tvo.setPcardid(new Long(2));
vo.setPocapassword(StringUtil.randomString());
// vo.setPocaid("12");
vo.setTblpcardtype(tvo);
dao.addPointCard(vo);
System.out.println(vo.getPocaid()+"--------");
}
*********************************************普通类
*********************************************Servlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException,
IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String action = StringUtil.trim(request.getParameter("action"));
if ("createCardType".equals(action)) {
this.doCreateCardType(request, out);
return;
}
if ("createCards".equals(action)) {
TblPointCardsVO vo = new TblPointCardsVO();
ITblPointCards dao = new TblPointCardsImpl();
TblPCardTypeVO tvo = new TblPCardTypeVO();
tvo.setPcardmoney("100");
tvo.setPcardid(new Long(2));
vo.setPocapassword(StringUtil.randomString());
// vo.setPocaid("12");
vo.setTblpcardtype(tvo);
dao.addPointCard(vo);//<----------------到这里就出错了
System.out.println(vo.getPocaid()+"--------");
Iterator it = dao.queryAllPointCard().iterator();
while (it.hasNext()) {
System.out.println(((TblPointCardsVO) it.next()).getPocapassword());
}
*********************************************Servlet
在这里调用dao.addPointCard(vo);的方法。很正常
在SERVLET里调用就出问题。
出错信息
21:20:43,812 ERROR [STDERR] org.hibernate.hql.ast.QuerySyntaxError: expecting "set", found 'as' near line 1, column 24 [update TblPointCardsVO as vo set vo.pocaid=? where vo.pocaid=?]
at org.hibernate