春季 JPA:如何在不丢失数据的情况下进行更新插入
2022-09-03 06:44:53
有没有办法在不存在的情况下插入新记录,如果存在,则更新记录而不会丢失旧数据?
这是我的服务层方法:
public void saveSample(Sample sample) {
Sample samplePersistent = sample;
if (sample.getId() != null) {
samplePersistent = sampleRepository.findOne(sample.getId());
Assert.notNull(samplePersistent, "Sample entity not found with id : " + sample.getId());
samplePersistent.setLocation(sample.getLocation());
samplePersistent.setName(sample.getName());
samplePersistent.setType(sample.getType());
...
...
}
samplePersistent.cloneAuditingInfoFrom(sample);
sampleRepository.save(sample);
}
我认为这是无用的方式。
Spring BeanUtils Class或@DynamicUpdate Annotation可以解决我的问题吗?