这个问题以前已经被问过一次,但解决方案并没有解决这个问题。我正在创建一个JUnit测试:
@Test
@Transactional
@Modifying
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);
}
上面的测试所调用的查询是:
@Query("UPDATE Material m SET m.inventory_count = ?2 WHERE m.id = ?1")
void updateMaterialInventory(Long id,int newInventoryAmount);
错误:
Caused by: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [UPDATE com.htd.domain.Material m SET m.inventory_count = ?2 WHERE m.id = ?1]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.errorIfDML(QueryTranslatorImpl.java:318)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:369)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:495)
... 55 more
@Transactional
。在存储库的查询方法中添加@Modifying
。 - Antoine Dahan