Java 在春季,mongoDB是否有@MappedSuperclass等价的注释?

Java 在春季,mongoDB是否有@MappedSuperclass等价的注释?,java,mongodb,spring-boot,spring-data-jpa,spring-data-mongodb,Java,Mongodb,Spring Boot,Spring Data Jpa,Spring Data Mongodb,在我的项目中,我们在一定程度上从SQL转向了NoSQL。 我想知道,我们如何在SpringDataMongo中将基类属性继承到子类中。 我知道如何在SpringJPAForSQL中实现这一点 例如, 下面是用@MappedSuperClass注释的BaseEntity父类 它的字段为id和version @MappedSuperclass public class BaseEntity { @Id @GeneratedValue private Long id;

在我的项目中,我们在一定程度上从SQL转向了NoSQL。 我想知道,我们如何在SpringDataMongo中将基类属性继承到子类中。 我知道如何在SpringJPAForSQL中实现这一点

例如, 下面是用@MappedSuperClass注释的BaseEntity父类 它的字段为id和version

@MappedSuperclass
public class BaseEntity {
 
    @Id
    @GeneratedValue
    private Long id;
 
    @Version
    private Integer version;
 
    //Getters and setters omitted for brevity
}
@MappedSuperclass
public class BaseEntity {
 
    @Id
    @GeneratedValue
    private Long id;
 
    @Version
    private Integer version;
 
    //Getters and setters omitted for brevity
}
实体可以扩展BaseEntity类并跳过声明@Id或@Version属性,因为它们是从基类继承的

@Entity(name = "Post")
@Table(name = "post")
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;
    }
}
如何在Mongo中实现相同的功能?比如说


在Mongo中,您不需要任何注释就可以做到这一点。Mongo本身将为您处理超类


只要在所有实体中扩展BaseEntity类,当您将实体读写到数据库时,所有实体都将具有BaseEntity类中的字段。这也适用于多级层次结构。i、 e.
Post extensed BaseEntity
BaseEntity extensed Entity
,在这种情况下,Post将同时具有BaseEntity和Entity class的字段。

我们如何获取子类属性?当您从存储库返回数据时,您将始终返回子类,所以你可以直接得到子类属性我面临这个问题,但我没有得到子类属性
@MappedSuperclass
public class BaseEntity {
 
    @Id
    @GeneratedValue
    private Long id;
 
    @Version
    private Integer version;
 
    //Getters and setters omitted for brevity
}
@Document(collection = "Post")
public class Post extends BaseEntity {
 
    private String title;
 
    //Rest of the code
}
@Document(collection = "PostComment")
public class PostComment extends BaseEntity {
 
    @ManyToOne(fetch = FetchType.LAZY)
    private Post post;
 
    private String review;
 
    //Getters and setters omitted for brevity
}