简体   繁体   中英

How to delete records in JPA with greater than condition?

I want to delete the records from a PGSQL database table(ContainerTypeRule) whose id(rule_id) is greater than 10000. I am creating a query in JPA like as follows

public void deleteAllTestRules(long startID){
    em.createQuery("DELETE FROM ContainerTypeRule WHERE rule_id > ?startID", ContainerTypeRule.class)
            .setParameter("startID", startID)
            .executeUpdate();
}

But I am getting the error like this.

javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing [DELETE FROM ContainerTypeRule WHERE rule_id > ?startID]. 
[46, 54] The positional input parameter ''{0}'' cannot use non-Integer characters.

How can I formulate correct query for 'id greater than 10000' condition in my JPA query.

You must use: not?

DELETE FROM ContainerTypeRule WHERE rule_id > :startID
  • The problem is in writing the query parameters.
  1. You can either use positional parameters and use '?' . ex: DELETE FROM ContainerTypeRule WHERE rule_id >:startID

  2. Or you can use named parameters and use ':' . ex: DELETE FROM ContainerTypeRule WHERE rule_id >?1

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