How to update only the entity attributes passed from @RequestBody using Spring Rest Controller and Spring Data JPA ?
Employee Entity:
@Entity
@Table(name = "employee")
@DynamicUpdate
public class Employee {
@Id
@Column(name = "employee_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer employeeId;
@Column(name = "employee_name")
private String employeeName;
@Column(name = "employee_desgn")
private String employeeDesgn;
@Column(name = "employee_email")
private String employeeEmail;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "company_id", referencedColumnName = "company_id")
private Company company;
}
Service Class Method:
public Employee updateEmployee(Employee employee) {
return employeeRepository.save(employee);
}
RequestBody:
{
"employeeId":4,
"employeeName":"Paul",
"employeeDesgn":"Lead"
}
Hibernate Update Query:
update employee set company_id=?, employee_desgn=?, employee_name=? where employee_id=?
Spring Data JPA is trying to set company_id as null to update even though I am not passing it through RequestBody ? But if I got entity from database using employee_id passed and then if I tried to save() then its working fine.
I would like to update only entity attributes which are passed through Request Body. How to handle this scenario ?