Not supported for DML operations with JPA update query
As far as I understand, you should not use @Transactional
annotation in the repository class. Find the below answer.
Service Impl class
import org.springframework.transaction.annotation.Transactional;
...
@Test
@Transactional
public void updateMaterialInventory() throws Exception{
// Initialize the database
materialRepository.saveAndFlush(material);
long id = material.getId();
materialRepository.updateMaterialInventory(id,UPDATED_INVENTORY_COUNT);
assertEquals(material.getInventory_count(), UPDATED_INVENTORY_COUNT, 0);
}
Repository class
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
...
@Modifying
@Query("UPDATE Material m SET m.inventory_count = ?2 WHERE m.id = ?1")
void updateMaterialInventory(Long id,int newInventoryAmount);
make sure to use the correct imports. hope this is helpful.
The @Modifying
annotation must be placed on the updateMaterialInventory
method, along to the @Query
annotation, to let Spring-data know that the query is not a query used to select values, but to update values.