1

I need to do bulk updates using id's in JPA but by updating different values for each row. Currently I am updating all the rows with same value as follows:

queryStm = "Update DUMMY set dlr=:dlr,time=:time,serverTime=:servertime  where id IN :arrids ";

Query query  = em.createNativeQuery(queryStm);
query.setParameter("arrids ",arrids );
query.setParameter("dlr", dlr);
query.setParameter("time", time);
query.setParameter("servertime", new Timestamp(new Date().getTime()));

query.executeUpdate();

But I need to update different value for time time=:time , is there any other way to do this.

I am using Postgres 9.6.2 version and using the following dependency

<dependency>
    <groupId>javax.ejb</groupId>
    <artifactId>javax.ejb-api</artifactId>
    <version>3.2</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.3.Final</version>
</dependency>
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
kirti
  • 4,499
  • 4
  • 31
  • 60
  • you will have to somehow determine which id should be updated with which value – XtremeBaumer May 16 '17 at 10:44
  • or if the time depends on the other values updated in the query just change the query logic – Zeromus May 16 '17 at 10:44
  • Possible duplicate of [How to efficiently update postgres using a tuple of the PK and a value?](http://stackoverflow.com/questions/18767115/how-to-efficiently-update-postgres-using-a-tuple-of-the-pk-and-a-value) – Naros May 16 '17 at 15:11
  • JPA is irrelevant since you're using native-SQL anyway and native-SQL is the only logical way to perform such a bulk operation. See my above comment for how to efficiently update postgres using a temporary table, tuple, and PK. – Naros May 16 '17 at 15:13

0 Answers0