首页 > 解决方案 > @Document(collection = "collectionName") 扩展另一个类时不起作用

问题描述

我正在使用 spring data mongoDB 开发一个 spring boot 应用程序。我有一个包含所有公共属性的公共类,我的其他类扩展了它,以便我在其中拥有这些公共属性。

@Getter
@Setter
@EntityListeners({AuditingEntityListener.class})
public abstract class AbstractAuditEntity implements Serializable {
    @CreatedBy
    @NotNull
    private Long createdBy;

    @LastModifiedBy
    @NotNull
    private Long updatedBy;

    @CreatedDate
    @NotNull
    private Instant createdDate;

    @LastModifiedDate
    @NotNull
    private Instant updatedDate;
}

现在当我在我的另一个类中扩展这个类时

@Document(collection = "consumer")
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class ConsumerEntity extends AbstractAuditEntity {

    @Id
    private ObjectId id;
}

在 mongo 数据库中创建的文档与我的类名相同,即“ConsumerEntity”,我如何让 @Document(collection = "consumer") 工作,以便在我的 mongo 数据库中将集合名称反映为“consumer”。谢谢你

标签: spring-data-mongodb

解决方案


推荐阅读