我有一个实体Customer
和 Spring 数据接口,CustomerRepository
如下所示:
public interface CustomerRepository extends JpaRepository<Customer,Long> {
Customer findCustomerByName(String name);
}
我将Customer
对象保存在数据库中,然后像这样更新一个字段:
customerRepository.save(new Customer("John", "Smith"));
Customer john = customerRepository.findCustomerByName("John");
john.setSurname("Barton");
customerRepository.flush();
customerRepository.findAll().forEach(System.out::println);
我不明白为什么会打印:Customer(id=1, name=John, surname= Smith )。
据我所知,Hibernate 使用dirty checking
机制来更新处于持久状态的实体。所以更改的姓氏应该在事务结束期间传播到数据库(但它不会 - 即使我将此代码分成两个@Transactional
方法)。难道我做错了什么?每次更改后我真的需要save
手动反对吗?为什么数据库中的姓氏字段没有更新?