Batch Update by different values using Spring JPA/Hibernate

Viewed 227

There is Java entity class:

class Call {
   int callId,
   String callStatus
}

callStatus posibly values : SETUP, INSTALLED, FINISHED

And table calls_statuses has values

callId callStatus
1       SETUP
2       INSTALLED
3       INSTALLED

Then i need change statuses for callId 1 and 2 as :

1       INSTALLED
2       FINISHED

The call with callId 3 left same.

Is it possible to make a batch update using Spring JPA/Hibernate?

1 Answers

Try something like this, a custom JPQL query would like:

    @Query("Update CallStatuses cs SET cs.callStatus = CASE WHEN (cs.callId IN :calls1) THEN :callStatusInstalled WHEN (cs.id IN :calls2) THEN :callStatusFinished END")
    batchUpdateCallStatus(@Param("calls1")List<int> calls1,
                          @Param("calls2") List<int> calls2,
                          @Param("callStatusInstalled") String callStatusInstalled, 
                          @Param("callStatusFinished") String callStatusFinished);

Posssible duplicate: JPQL - Update with Multiple where conditions

Related