在对mongo实体使用继承时,如何正确处理基类的更新?
基类:
@Document
public class User {
@Id
private String id;
@UniqueElements
protected String uid;
protected String photoUrl;
protected Address homeAddress;
protected Gender gender;
protected List<Kid> kids;
protected GeneralInfo generalInfo;
protected List<Address> addresses;
protected String instanceId;
public User() {
}
public User(User user) {
this.uid = user.getUid();
this.photoUrl = user.getPhotoUrl();
this.homeAddress = user.getHomeAddress();
this.gender = user.getGender();
this.kids = user.getKids();
this.generalInfo = user.getGeneralInfo();
this.addresses = user.getAddresses();
this.instanceId = user.getInstanceId();
}
}
孩子:
@Document
public class Sitter extends User{
private int childCareExperienceYrs;
private AgeRange preferredKidsAgeRange;
private List<Skill> skills;
private String talents;
private LivingSpace livingSpace;
@JsonProperty
private boolean pets;
@JsonProperty
private boolean hasCar;
@JsonProperty(value = "available")
private boolean available;
}
@Document
public class Parent extends User{
}
从用户继承的属性保存在子文档中。 当我更新User属性时,子表不会更新。
例如,当更改用户地址时,“ Sitter”文档仍将具有旧地址。
如何对基类进行任何更改以同样反映子类?
这种情况是否需要在更改基类时对每个孩子进行更新查询?