在Spring中,是否有适用于MongoDB的 @MappedSuperclass 等效注解?

huangapple go评论71阅读模式
英文:

is there a @MappedSuperclass equivalent annotation for mongoDB in spring?

问题

在我的项目中,我们在某种程度上从关系型数据库(SQL)迁移到NoSQL。我想知道,在Spring Data Mongo中,如何将BaseClass的属性继承到子类中。

示例:
以下是一个被注解为@MappedSuperclass的BaseEntity父类,它拥有id和version作为它的字段。

@MappedSuperclass
public class BaseEntity {
 
    @Id
    @GeneratedValue
    private Long id;
 
    @Version
    private Integer version;
 
    //省略了getter和setter以保持简洁
}

实体可以扩展BaseEntity类,并且可以省略声明@Id或@Version属性,因为它们会从基类中继承。

@Document(collection = "Post")
public class Post extends BaseEntity {
 
    private String title;
 
    @OneToMany
    private List<Comment> comments = new ArrayList<>();
 
    @OneToOne
    private PostDetails details;
 
    @ManyToMany
    @JoinTable(//某个关联表)
    private Set<Tag> tags = new HashSet<>();
 
    //省略了getter和setter以保持简洁
 
    public void addComment(PostComment comment) {
        comments.add(comment);
        comment.setPost(this);
    }
 
    public void addDetails(PostDetails details) {
        this.details = details;
        details.setPost(this);
    }
 
    public void removeDetails() {
        this.details.setPost(null);
        this.details = null;
    }
}
@Document(collection = "PostComment")
public class PostComment extends BaseEntity {
 
    @DBRef
    private Post post;
 
    private String review;
 
    //省略了getter和setter以保持简洁
}

我该如何在Mongo中实现相同的功能?例如:

@MappedSuperclass
public class BaseEntity {
 
    @Id
    @GeneratedValue
    private Long id;
 
    @Version
    private Integer version;
 
    //省略了getter和setter以保持简洁
}
@Document(collection = "Post")
public class Post extends BaseEntity {
 
    private String title;
 
    //其余代码
}
@Document(collection = "PostComment")
public class PostComment extends BaseEntity {
 
    @DBRef
    private Post post;
 
    private String review;
 
    //省略了getter和setter以保持简略
}
英文:

In my project, we are moving from SQL to NoSQL to a certain extent.
I wanted to know, how can we inherit BaseClass properties into child classes in spring data mongo.
I know how to do it in Spring JPA for SQL.

Example,
Below is BaseEntity parent class which is annotated with @MappedSuperClass
It has id and version as its fields.

@MappedSuperclass
public class BaseEntity {
 
    @Id
    @GeneratedValue
    private Long id;
 
    @Version
    private Integer version;
 
    //Getters and setters omitted for brevity
}

entities can extend the BaseEntity class and skip declaring the @Id or @Version properties since they are inherited from the base class.

@Entity(name = &quot;Post&quot;)
@Table(name = &quot;post&quot;)
public class Post extends BaseEntity {
 
    private String title;
 
    @OneToMany
    private List comments = new ArrayList();
 
    @OneToOne
    private PostDetails details;
 
    @ManyToMany
    @JoinTable(//Some join table)
    private Set tags = new HashSet();
 
    //Getters and setters omitted for brevity
 
    public void addComment(PostComment comment) {
        comments.add(comment);
        comment.setPost(this);
    }
 
    public void addDetails(PostDetails details) {
        this.details = details;
        details.setPost(this);
    }
 
    public void removeDetails() {
        this.details.setPost(null);
        this.details = null;
    }
}
@Entity(name = &quot;PostComment&quot;)
@Table(name = &quot;post_comment&quot;)
public class PostComment extends BaseEntity {
 
    @ManyToOne(fetch = FetchType.LAZY)
    private Post post;
 
    private String review;
 
    //Getters and setters omitted for brevity
}

How can I implement same thing in Mongo? For example

@MappedSuperclass
public class BaseEntity {
 
    @Id
    @GeneratedValue
    private Long id;
 
    @Version
    private Integer version;
 
    //Getters and setters omitted for brevity
}
@Document(collection = &quot;Post&quot;)
public class Post extends BaseEntity {
 
    private String title;
 
    //Rest of the code
}
@Document(collection = &quot;PostComment&quot;)
public class PostComment extends BaseEntity {
 
    @ManyToOne(fetch = FetchType.LAZY)
    private Post post;
 
    private String review;
 
    //Getters and setters omitted for brevity
}

答案1

得分: 5

你不需要在Mongo中做任何注释。Mongo会为您处理超类。

只需在所有实体中扩展BaseEntity类,所有实体在将实体读写到数据库时都将具有来自BaseEntity类的字段。这在多级层次结构中也适用。例如,Post扩展BaseEntityBaseEntity扩展Entity,在这种情况下,Post将具有来自BaseEntity和Entity类的字段。

英文:

You do not need any annotation to do that in Mongo. Mongo itself will take care of superclass for you.

Just extend BaseEntity class in all your entities, all entities will have fields from BaseEntity class when you read and write entities to database. This also works at multilevel hierarchy. i.e. Post extends BaseEntity, BaseEntity extends Entity, in this case Post will have fields from both BaseEntity and Entity class.

huangapple
  • 本文由 发表于 2020年10月23日 15:36:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64495824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定