简体   繁体   中英

Hibernate batch delete not working?

I am trying to batch delete data from table . For that am setting parameter list for deletion.

    ArrayList<Integer> ids = new ArrayList<Integer>();
    ids.add(1);
    ids.add(2);
    ids.add(3);
    ids.add(4);

    Session session1 = sfactory.openSession();
    Transaction txn1 = session1.beginTransaction();

    Query query = session1.createQuery("delete from QueueData tbl where tbl.iID in =:id");
    query.setParameter("id", ids);

    int i = query.executeUpdate();
    txn1.commit();

I am getting exception while executing statement.

   SEVERE: line 1:61: unexpected token: =
   Exception in thread "main"
   org.hibernate.hql.ast.QuerySyntaxException:    
   unexpected token: = near line 1, column 61 [delete from   
   hibernetexamples.QueueData tbl where tbl.iID in =:id]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:258)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at hibernetexamples.HibernetExamples.main(HibernetExamples.java:104)

You should be aware of basic sql.

IN condition syntax

Your delete query should look like:

"delete from QueueData tbl where tbl.iID in (:id)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM