Java 未正确设置包含spring数据的Mongo集合名称

Java 未正确设置包含spring数据的Mongo集合名称,java,spring,mongodb,Java,Spring,Mongodb,我试图为我的mongo集合使用基类,然后让集合名称来自派生类;但是,集合名称始终只是实体(在本例中,不是派生的) 我有以下抽象类: public abstract class Entity { @Id private String id; public String getId() { return this.id; } public void setId(String id) { this.id = id;

我试图为我的mongo集合使用基类,然后让集合名称来自派生类;但是,集合名称始终只是
实体
(在本例中,不是
派生的

我有以下抽象类:

public abstract class Entity {

    @Id
    private String id;

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
和派生类,如:

// I've also tried @TypeAlias("Derived")
@Document(collection = "Derived")
public class Derived extends Entity {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
使用类似以下的存储库:

@Component
public interface Repository<T extends Entity> extends MongoRepository<T, String> {

    T findById(String id);

}
@组件
公共接口存储库扩展了MongoRepository{
T findById(字符串id);
}

遵循此工作示例。 让我们声明一个基本文档模型,每个文档模型都必须从中继承

import java.math.biginger;
导入org.springframework.data.annotation.Id;
公共类基本文档{
@身份证
私有整数id;
public BigInteger getId(){
返回id;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj){
返回true;
}
if(this.id==null | | obj==null | |!(this.getClass().equals(obj.getClass())){
返回false;
}
BaseDocument,它=(BaseDocument)obj;
返回this.id.equals(that.getId());
}
@凌驾
公共int hashCode(){
返回id==null?0:id.hashCode();
}
}
例如,我声明了我的代码模型请注意,如果集合名称与类名不同,则可以使用@Document annotation设置集合名称

import org.springframework.data.mongodb.core.mapping.Document;
导入org.springframework.data.mongodb.core.mapping.Field;
@文件(收集=“代码”)
公共类代码扩展了BaseDocument{
@字段(“代码”)
私有字符串码;
@字段(“假日”)
私人假期;
公共代码(字符串代码、字符串假日){
超级();
this.code=代码;
这个节日=假日;
}
公共字符串getCode(){
返回码;
}
公共无效设置码(字符串码){
this.code=代码;
}
公共字符串getHoliday(){
返程假期;
}
公众假期(连续假期){
这个节日=假日;
}
@凌驾
公共字符串toString(){
返回“Code[Code=“+Code+”,holiday=“+holiday+”]”;
}
}
此时,您可以使用MongoOperations和其他类轻松地使用这些类,以便对数据库和集合进行操作。MongoOperations提供了对集合进行操作所需的所有方法,例如:findOne、findAll等。 需要明确的是,您可以通过以下方式使用MongoOperations:

MongoOperations mongoOps=newmongotemplate(newsimplemongodbfactory(newmongoclient(),“yourDBname”);
强烈建议实现一个对集合进行操作的服务类,如下面的服务类,而不是直接对集合进行操作:

import java.util.List;
导入org.springframework.data.mongodb.core.query.Criteria;
导入org.springframework.data.mongodb.core.query.query;
公共类CodesService扩展BaseService实现BaseOperations{
@凌驾
public BaseDocument findOne(长id){
查询查询=新查询(条件,其中(“代码”)为(“8”);
返回this.mongoOps.findOne(query,Code.class);
}
@凌驾

公共列表遵循这个工作示例。 让我们声明一个基本文档模型,每个文档模型都必须从中继承

import java.math.biginger;
导入org.springframework.data.annotation.Id;
公共类基本文档{
@身份证
私有整数id;
public BigInteger getId(){
返回id;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj){
返回true;
}
if(this.id==null | | obj==null | |!(this.getClass().equals(obj.getClass())){
返回false;
}
BaseDocument,它=(BaseDocument)obj;
返回this.id.equals(that.getId());
}
@凌驾
公共int hashCode(){
返回id==null?0:id.hashCode();
}
}
例如,我声明了我的代码模型。请注意,如果集合名称与类名不同,可以使用@Document annotation设置集合名称

import org.springframework.data.mongodb.core.mapping.Document;
导入org.springframework.data.mongodb.core.mapping.Field;
@文件(收集=“代码”)
公共类代码扩展了BaseDocument{
@字段(“代码”)
私有字符串码;
@字段(“假日”)
私人假期;
公共代码(字符串代码、字符串假日){
超级();
this.code=代码;
这个节日=假日;
}
公共字符串getCode(){
返回码;
}
公共无效设置码(字符串码){
this.code=代码;
}
公共字符串getHoliday(){
返程假期;
}
公众假期(连续假期){
这个节日=假日;
}
@凌驾
公共字符串toString(){
返回“Code[Code=“+Code+”,holiday=“+holiday+”]”;
}
}
此时,您可以使用MongoOperations和其他类轻松地使用这些类,以便对数据库和集合进行操作。MongoOperations提供了对集合进行操作所需的所有方法,如findOne、findAll等。 需要明确的是,您可以通过以下方式使用MongoOperations:

MongoOperations mongoOps=newmongotemplate(newsimplemongodbfactory(newmongoclient(),“yourDBname”);
强烈建议实现一个对集合进行操作的服务类,如下面的服务类,而不是直接对集合进行操作:

import java.util.List;
导入org.springframework.data.mongodb.core.query.Criteria;
导入org.springframework.data.mongodb.core.query.query;
公共类CodesService扩展BaseService实现BaseOperations{
@凌驾
public BaseDocument findOne(长id){
瞿
public interface SomeInterfaceName extends Repository<Derived>{
}