使用setParameter或setParametrList奇怪性删除数据HQL

时间:2012-08-09 06:11:52

标签: java hibernate oracle10g hql

我有一个简单的表,并尝试按ID数组删除数据。

Integer[] arrayIDs = {1,2,3}; //External ids as parameteres
    Query deleteItemsQuery = session.createQuery("DELETE Items WHERE id IN (:idsDeletingItems)"); //(in this row exception)
deleteItemsQuery.setParameterList("idsDeletingItems", arrayIDs);
deleteItemsQuery.executeUpdate();

我的应用程序抛出异常

我也尝试删除1个参数。例如

Query deleteItemsQuery = session.createQuery("DELETE Items WHERE id = '1'");
deleteItemsQuery.executeUpdate();

使用该变体没问题。

但是当我接下来做的时候我也有例外

Integer myID = 1;
Query deleteItemsQuery = session.createQuery("DELETE Items WHERE id = (:myID)");
    deleteItemsQuery.setParameter("myID", myID);
    deleteItemsQuery.executeUpdate();

我使用这个方言org.hibernate.dialect.Oracle10gDialect。有什么想法吗?

异常

exception 

javax.servlet.ServletException: java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I
    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:295)
    org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
    org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:113)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    com.epam.testapp.presentation.filter.CharsetFilter.doFilter(CharsetFilter.java:44)
    com.epam.testapp.presentation.filter.CommandFilter.doFilter(CommandFilter.java:58)


root cause 

java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I
    org.hibernate.hql.internal.ast.HqlSqlWalker.generateNamedParameter(HqlSqlWalker.java:956)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.parameter(HqlSqlBaseWalker.java:4821)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1347)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4297)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3772)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1947)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:794)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.deleteStatement(HqlSqlBaseWalker.java:443)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:263)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
    org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
    org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:219)
    org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:197)
    org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1736)
    com.epam.testapp.database.NewsHibernateDAO.remove(NewsHibernateDAO.java:89)
    com.epam.testapp.presentation.action.NewsAction.delete(NewsAction.java:150)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
    org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
    org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:113)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

2 个答案:

答案 0 :(得分:1)

像这样做

Integer myID = 1;
Query deleteItemsQuery = session.createQuery("DELETE Items WHERE id =:myID");
    deleteItemsQuery.setParameter("myID", myID);
    deleteItemsQuery.executeUpdate();

并确保“:”和“myID”之间没有空格,否则会出现另一个错误。

希望它有所帮助; - )

答案 1 :(得分:1)

我也遇到过这个问题,并通过在我的参数中添加开始和结束括号而不是直接将它放在HQL中来解决它。