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即可工作