Spring JpaRepository不会更新表

时间:2017-03-20 12:23:16

标签: java spring hibernate spring-data-jpa

我有下一个JPARepo代码:

public interface MyObjectRepository extends JpaRepository<MyObject, UUID> {
  @Modifying
  @Query("UPDATE MyObject obj SET obj.deleted = true WHERE obj.objectId = ?1")
  void deleteById(UUID uuid);
}

为其服务而致:

@Service
@Slf4j
@Transactional(readOnly = true)
public class MyService {
@Autowired
  private MyObjectRepository myObjectRepository;

  // some methods here

  @Transactional
  public void deleteMyObject(MyObject myObject) {
    ** first way **
    myRepository.deleteById(myObject.getMyObjectId());
    ** second way **
    myObject.setDeleted(true);
    myObjectRepository.save(myObject);
  }

  //some methods here

}

两种更新方式只执行一次。下次我调用方法时,默认情况下,删除列中的实体值为false,更新后它保持为false。但是在调试时我会逐步查看表格,我看到删除列设置为true,当我发布调试时,删除标志再次设置为false。 是缓存还是不正确的注释?

1 个答案:

答案 0 :(得分:1)

我认为班级@Transactional(readOnly = true)适用于所有方法,您可以在方法级别将其更改为@Transactional(readOnly = false)

 @Transactional(readOnly = false)
  public void deleteMyObject(MyObject myObject) {