HQL的插入操作
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: values near line 1, column 86 [ insert into mail_receive(…… at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54) at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47) at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82) at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:286) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:184) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138) at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101) at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80) at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124) at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156) at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135) at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770) at util.MailUtil.saveReceiveMail(MailUtil.java:634) at util.MailUtil.recvImapMail(MailUtil.java:594) at mail.MailBusiness.test(MailBusiness.java:408) at mail.MailThread.run(MailThread.java:28)
解决:
1、HQL不支持insert插入,使用对象session.save(st);
2、将“dbSession.createQuery(sql);”换成“dbSession.createSQLQuery(sql);”。
如下:
sql=”insert into table (id,name,sax) values(1,’aa’,’b’)”;
Query query= dbSession.createSQLQuery(sql);
query.executeUpdate();
我有两个梦想,一是娶一美丽贤惠的姑娘,二是有一稳定踏实的工作。