简体   繁体   English

春季数据-实体未更新

[英]Spring data - entity not updated

I have an entity Customer and Spring data interface CustomerRepository shown below: 我有一个实体Customer和Spring数据接口CustomerRepository如下所示:

public interface CustomerRepository extends JpaRepository<Customer,Long> {
    Customer findCustomerByName(String name);
}

I save Customer object in the database and then update one field like this: 我将Customer对象保存在数据库中,然后更新一个字段,如下所示:

customerRepository.save(new Customer("John", "Smith"));
Customer john = customerRepository.findCustomerByName("John");
john.setSurname("Barton");
customerRepository.flush();
customerRepository.findAll().forEach(System.out::println);

I don't understand why it prints: Customer(id=1, name=John, surname= Smith ). 我不明白为什么打印:Customer(id = 1,name = John,surname = Smith )。

As far as I know, Hibernate uses dirty checking mechanism to update entities in persistent state. 据我所知,Hibernate使用dirty checking机制来更新处于持久状态的实体。 So changed surname should be propagated to the database during end of transaction (but it does not - even if I separate this code into two @Transactional methods). 因此,更改的姓氏应在事务处理结束时传播到数据库(但是,即使我将此代码分成两个@Transactional方法,也不能这样做)。 Am I doing something wrong? 难道我做错了什么? Do I really need to save object manually after each change? 每次更改后,我真的需要手动save对象吗? Why surname field is not updated in the database? 为什么姓氏字段未在数据库中更新?

@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepoTest {
    @Autowired
    private CustomerRepository customerRepository;

    @Test
    //NOTE: No @Transactional
    public void testSaveFails() throws Exception {
        customerRepository.save(new Customer("John", "Smith"));
        Customer john = customerRepository.findCustomerByName("John");
        john.setSurname("Barton");
        customerRepository.flush();
        customerRepository.findAll().forEach(System.out::println);
    }

    @Test
    @Transactional
    public void testSaveWorks() throws Exception {
        customerRepository.save(new Customer("John", "Smith"));
        Customer john = customerRepository.findCustomerByName("John");
        john.setSurname("Barton");
        //customerRepository.flush(); Flush is not necessary
        customerRepository.findAll().forEach(System.out::println);
    }

}

To explain: Hibernate keeps a cache of objects it loaded during a transaction. 解释一下:Hibernate保留了在事务期间加载的对象的缓存。 When a find method is executed, the id of a newly loaded object is compared to the id of the object in this cache, if found, the cached version is used. 当执行find方法时,会将新加载的对象的ID与该缓存中的对象的ID进行比较(如果找到),则使用缓存的版本。

  • This is why the version with @Transactional works. 这就是使用@Transactional的版本起作用的原因。
  • Also, it explains why flush is not necessary - it only forces the values to be written before the transaction ends. 此外,它解释了为什么不需要刷新-它仅强制在事务结束之前写入值。

If you miss the @Transactional (assuming auto-commit on the underlying transaction, which is most likely the case): 如果您错过了@Transactional (假设自动提交基础事务,则很可能是这种情况):

  • You save the entity in one transaction 您将实体保存在一笔交易中
  • Reload it with findCustomerByName, but it immediately gets detached 用findCustomerByName重新加载它,但是它立即被分离
  • you modify the detached entity - no save 您修改分离的实体-不保存
  • you reload the entries in another transaction, and you don't see your update 您在另一个事务中重新加载条目,但看不到更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Data JPA 合并更新的实体 - Spring Data JPA Merge Updated Entity 实体未在实体侦听器@PostPersist(Spring 数据 jpa)上更新 - The entity is not updated on entity listener @PostPersist (Spring data jpa) 如何使用条件(where 子句)更新实体并在 spring 数据 jpa 中的方法响应中获取更新的实体 - How to update an entity with a condition(where clause) and get the updated entity in method response in spring data jpa Spring-Data-Jpa AuditingEntityListener createdDate在保存现有实体时更新 - Spring-Data-Jpa AuditingEntityListener createdDate updated upon saving existing entity Spring data - @Modifying(clearAutomatically = true , flushAutomatically = true) 导致另一个实体不被更新 - Spring data - @Modifying(clearAutomatically = true , flushAutomatically = true) causing another entity not to be updated 实体/行仅在春季/休眠时才更新? - Entity/row is updated only sometimes with Spring/Hibernate? 如果在不发出查询的情况下未知实体ID的情况下,是否可以更新实体而不是使用spring-data-jpa插入? - Can an entity be updated instead of being inserted using spring-data-jpa when entity ID is not known without issuing a query? Spring 数据不保存实体 - Spring Data not saving an entity Spring 数据实体同步 - Spring Data entity synchronization 在Spring Data中分离实体 - Detach entity in Spring Data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM