I'm creating a SpringBoot/Angular 8 Application and am having some issues when attempting to update the decoupled frontend/backend objects. When I send a json post request containing the angular model, the @JsonIgnore
'd or otherwise missing values are updated to null instead of being ignored.
Related Issue:
This stack overflow question is closely related, and the recommended solution does work, but it breaks/bypasses a bunch of the Jackson/Hibernate annotations (e.g. @Inheritance(strategy=InheritanceType.JOINED)
and @Transient
), so I would like to look for alternative solutions if possible:
hibernate partially update when entity parsed with json
Example Java POJO
@Entity
@Table(name = "users")
@DynamicUpdate
public class User implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String a;
@Column
private String b;
@Column (nullable = false)
@JsonIgnore
private String c;
// standard getters and setters
}
Example Java Rest API
@RequestMapping(value = "/api/user", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<User> saveOrUpdate(@RequestBody User user, BindingResult result, HttpServletRequest request) {
if(user.getId()!=null){
user=ws.update(user);
}else{
user=us.create(user);
}
return new ResponseEntity<>(user,HttpStatus.OK);
}
Example Angular Service Method
saveUser(user:User): Observable<User>{
const body = JSON.stringify(user);
return this.http.post<User>('/api/user', body).pipe(map( response => response));
}
Existing Database model:
{
id:1,
a:"some value a"
b:"some value b"
c:"some value c"
}
Angular Post Body:
{
id:1,
a:"some value a2"
b:"some value b2"
}
Expected Hibernate Query:
update users set a=?,b=? where id=?
Actual Hibernate Query:
update users set a=?,b=?,c=? where id=?
C, in this case, is a null value.