我有以下抽象类:
public abstract class StandardTimeStamp {
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
@JsonIgnore
private Date lastUpdated;
@PreUpdate
public void generatelastUpdated() {
this.lastUpdated = new Date();
}
public Date getLastUpdated() {
return lastUpdated;
}
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个实体是一个子类,我需要将lastUpdate值发送到浏览器,所以我需要覆盖@JsonIgnore.我尝试过一些东西,下面的代码就是其中之一:
public class MyEntity extends StandardTimestamp implements Serializable {
@Column(name = "LastUpdated")
private Date lastUpdated;
@JsonIgnore(false)
@Override
public Date getLastUpdated() {
return lastUpdated;
}
}
Run Code Online (Sandbox Code Playgroud)
这确实在JSON响应上添加了lastUpdated属性,但是当它在数据库中不为null时,其值为null.我在子类中有其他日期可以工作,但它们不会被超类中的@IgnoreJson隐藏.
有什么想法吗?
我一直在深入研究 MongoDB,并发现了一种特别有趣的模式来存储文档之间的关系。此模式涉及包含引用子文档的 id 数组的父文档,如下所示:
//Parent Schema
export interface Post extends mongoose.Document {
content: string;
dateCreated: string;
comments: Comment[];
}
let postSchema = new mongoose.Schema({
content: {
type: String,
required: true
},
dateCreated: {
type: String,
required: true
},
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }] //nested array of child reference ids
});
Run Code Online (Sandbox Code Playgroud)
和被引用的孩子:
//Child Schema
export interface Comment extends mongoose.Document {
content: string;
dateCreated: string;
}
let commentSchema = new mongoose.Schema({
content: {
type: String,
required: true
},
dateCreated: { …Run Code Online (Sandbox Code Playgroud) inheritance ×1
jackson ×1
java ×1
javascript ×1
mongodb ×1
mongoose ×1
node.js ×1
relationship ×1