user4919313
user4919313

Reputation: 125

Conditionally deletion with list parameter with org.springframework.data.jpa.repository.Query

I was trying to delete some rows with a list as parameter and with these conditions:

@Query("DELETE Entity where col1=:key.val1 and col2 =:key.val2 and col3=:key.val")
@Transactional
@Modifying
public int deleteEntity(@Param("key") List<EntityKey> key );

But when i trying to start the app with spring boot it returns me a null pointer at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery.

Is there any way to do it?

Upvotes: 0

Views: 440

Answers (1)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60045

I don't think you can use this way in annotation, you have to create your own code to delete this elements :

//You should not use OBJECT_NAME.FIELD_NAME in the query
Query query = em.createQuery("DELETE FROM Entity e where e.col1=:val1 "
                                    + "and e.col2 =:val2 and e.col3=:val3");

//because you have to delete many values, you need a loop
for(EntityKey entity : listEntities){

   //set the necessary parameters to the query
   query.setParameter("val1", entity.val1);
   query.setParameter("val2", entity.val2);
   query.setParameter("val3", entity.val3);

   query.executeUpdate();//execute update to delete the record
}

Upvotes: 1

Related Questions