带有@Version的Spring-data-mongodb DbRef延迟导致org.springframework.dao.DuplicateKeyException

时间:2019-05-16 08:06:21

标签: spring-boot spring-data-mongodb

Spring-boot 2.1.4, Spring-Data-Mongo 2.1.6, 加载并重新保存延迟加载的内容时,出现DuplicateKeyException。

@Document
public class User {
   @Id
   private String id;
   @Version
   private Long version;
   @DBRef(lazy = true)
   private Role role;
}

@Document
public class Role {
    @Id
    private String id;
    @Version
    private Long version;  
}
User user = userRepository.findById("1234");
roleRepository.save(user.getRole()); //org.springframework.dao.DuplicateKeyException

它无需lazy = true即可工作

0 个答案:

没有答案