Can I use Spring projections to modify and persist content of existing entities?
My goal is to update only a specific field. Any other fields of that row in the database should remain untouched. Is that possible with projections?
Or is a projection always only readonly?
public void update() {
PersonProjection p = repository.findByLastname("Doe");
p.setLastname("test");
repository.save(p); //how can I save only that field?
}
@Entity
public class Person {
@Id private long id;
private String firstname, lastname, age;
}
interface PersonProjection {
String getLastname();
void setLastname(String lastname);
}
interface PersonRepository extends CrudRepository<Person, Long> {
PersonProjection findByLastname(String lastname);
}
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections