我目前正在开发一个使用 mongojack 将对象存储在 mongoDB 中的 java 应用程序。一切正常,直到我开始在对象中实现一些帮助方法以直接返回嵌套对象而不是 DBRef。
public class Dish {
private String id =null;
private String name;
private DBRef<User,String> author;
@JsonCreator
public Dish(@ObjectId @JsonProperty("_id") String id,
@JsonProperty("name") String name,
@JsonProperty("author") DBRef<User, String> author)){
this.id = id;
this.name = name;
this.author = author;}
@ObjectId
@Id
public String getId() {
return id;
}
@JsonProperty
public String getName() {
return name;
}
@JsonProperty
public DBRef<User, String> getAuthor() {
return author;
}
@JsonIgnore
public User getAuthorExtracted() {
return author.fetch();
}
但是,添加最后一个方法会导致将 authorExtracted 添加到 mongoDB 中保存的 JSON。无论如何在实际菜肴中放置一些这样的辅助方法,而不会将对象保存到数据库中?