小编Sum*_*ani的帖子

Spring中的mongoDB是否有@MappedSuperclass等效注释?

在我的项目中,我们正在一定程度上从 SQL 转向 NoSQL。我想知道,我们如何将 BaseClass 属性继承到 spring data mongo 中的子类中。我知道如何在 Spring JPA for SQL 中执行此操作。

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

@MappedSuperclass
public class BaseEntity {
 
    @Id
    @GeneratedValue
    private Long id;
 
    @Version
    private Integer version;
 
    //Getters and setters omitted for brevity
}
Run Code Online (Sandbox Code Playgroud)

实体可以扩展 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 …
Run Code Online (Sandbox Code Playgroud)

java mongodb spring-data-jpa spring-data-mongodb spring-boot

3
推荐指数
1
解决办法
2978
查看次数