使用 Repository Save 或 SaveAll 方法部分插入更新 Hibernate JPA 模型,模型中没有 @Transient 或 @DynamicUpdate 注释

问题描述

我有我的模型

@Entity
public class Person{
@Id
@Column(name = "id")
String Id;
@Column(name = "name")
String name;
@Column(name = "location")
String location;
@Column(name = "status")
String status;
}

我有我的仓库:

@Repository
public interface PersonRepository extends CrudRepository<Person,Integer>{

}

如何只更新 Person 位置而不更新 Person 状态:


@Autowired
PersonRepository personRepository;

Person p1 = new Person();
p1.setId(1);
p1.setName("peter");
p1.setLocation("1231,1423")

personRepository.save(p1);//here it should insert update 'name' or 'location' without updating 'status' but is overwriting 'status' with 'null' when is not set in the Person model and I don't want it to override the 'status' with 'null' when is not set in Person

在我在“状态”字段中使用 @Transient 之前,它运行良好,但我的团队负责人告诉我不能为此使用 @Transient 注释。

我还发现我可以在 Person 模型中使用 @DynamicUpdate 注释,但它会导致性能问题,所以我想避免它,因为那样。

此外,我应该能够对人员列表进行部分更新:

@Autowired
PersonRepository personRepository;

Person p1 = new Person();
p1.setId(1);
p1.setName("peter");
p1.setLocation("1231,1423")

List<Person> listPerson = new ArrayList<Person>();
listPerson.add(p1);

personRepository.saveAll(listPerson);//here it should update all the Person fields except for the 'status' and it shouldn't override the status with 'null' when is not set in the Person model


我只是想找到一种方法,当我没有像下面的代码那样在模型中提供它时,避免使用“空”值覆盖/更新人员“状态”,但我希望能够更新它不是“空”值。


@Autowired
PersonRepository personRepository;

Person p1 = new Person();
p1.setId(1);
p1.setName("peter");
p1.setLocation("1231,1423")

personRepository.save(p1);// here is updating with null the Person status when the status is not set in the Person model before calling the Repository save method but I don't want to update with 'Nulls'

但是当我在模型中定义它或不为空时,我仍然希望能够更新状态,例如:

@Autowired
PersonRepository personRepository;

Person p1 = new Person();
p1.setId(1);
p1.setName("peter");
p1.setLocation("1231,1423");
p1.setStatus("offline");//here I want to update the status for an existing record to 'offline'

personRepository.save(p1);

我知道我是否使用“findBy()”、“modify”和“save()”方法效果很好,但我想避免先调用“findBy()”,因为我有数千条记录,我想避免对数据库调用次数。我知道以下方法有效的示例:

// find the exist entity into table        
Person person = personRepository.findById(1);
// update peter to peter2
person.setName("peter2");
// update peter2 into table
personRepository.save(person);

请帮忙。

解决方法

您可以在 updatable = false 中设置 @Column(name = "status",updatable = false) 属性。

这意味着当您更新实体时,它将忽略 status 字段。

当你第一次save()实体时,hibernate sql看起来像这样:

insert into person (location,name,status,id) values (?,?,?)

当你 save() 存在的实体时,hibernate sql 看起来像这样:

update person set location=?,name=? where id=?

基本上我希望能够只更新我在模型中提供的值,而不用 null 更新我使用“save”或“saveAll”存储库方法时未提供的值

// find the exist entity into table        
Person person = personRepository.findById(1)
                .orElseThrow(EntityNotFoundException::new);
        // update peter to peter2
        person.setName("peter2");
        // update peter2 into table
        personRepository.save(person);