1

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.

1 Answers1

0

You can set the updatable = false attribute in the @Column(name = "status", updatable = false).

This mean when you update the entity, it will ignore the status field.

When you save() the entity for the first time, hibernate sql look like this:

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

When you save() the entity that exists, hibernate sql look like this:

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

basically I want to be able to update only the values I provided in the model without updating with null the values not providing when I use the "save" or "saveAll" repository methods

// 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);
Changemyminds
  • 1,147
  • 9
  • 25
  • Can this solution still allow me to update the 'status' when the record already exist with an status? basically I want to be able to update only the values I provided in the model without updating with null the values not providing when I use the "save" or "saveAll" repository methods. – user1895917 Jul 02 '21 at 16:50
  • No. If you just want update some field of exist record, you can just use `findById` and update some field value and `save` it that avoid updating with null value. – Changemyminds Jul 02 '21 at 17:02
  • but I don't want to use "findBy()" first because if I update thousands of records I will have to first 'find' thousands of records then 'modify' and then 'save'. I dont want to use "find, modify, save" because I want to reduce the number of calls to the database but I don't want the "save" method to update with "nulls". – user1895917 Jul 02 '21 at 17:14
  • Ok ... I finally understand your question. It seems like this question https://stackoverflow.com/questions/27818334/jpa-update-only-specific-fields – Changemyminds Jul 02 '21 at 17:24
  • I think hibernate really makes difficult to perform partial update on the Models without using the 'findby()', Modify and "save()" approach. There should be an annotation that says don't update with nulls or something like that. – user1895917 Jul 02 '21 at 17:34