I have my model
@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;
}
I have my repository:
@Repository
public interface PersonRepository extends CrudRepository<Person, Integer>{
}
How can I update only the Person location without updating the Person status:
@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
Before I was using @Transient in the 'status' field and it was working very well but I was told I cannot use the @Transient annotation for this by my team lead.
I also found that I can use the @DynamicUpdate annotation in the Person model but it can cause performance issues so I want to avoid it because that.
Additionally I should be able to do this partial update with list of person:
@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
I simply want to find a way to avoid overwriting/updating with 'Null' value the Person 'status' when I don't provide it in the Model like in the below code but I want to be able to update it will not 'Null' values.
@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'
But I want to still be able to update the status when I define it in the model or when is not null, example:
@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);
I know if I use the "findBy()","modify" and "save()" approach works wells but I want to avoid calling 'findBy()' first because I have thousands of records and I want to avoid the number of calls to the database. example I know below approach works:
// 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);
Please help.