首页 文章

@Document(collection =“Test”)在MongoRepository中不起作用 - Spring数据 - mongodb

提问于
浏览
1

我正在尝试使用java的泛型创建通用mongo存储库 . 我已经定义了如下的存储库 .

public interface ICentroRepository<T extends Serializable, ID extends 
Serializable> extends MongoRepository<T, ID> {  
}

我正在定义文档实体如下

@Document(collection = "mycollectionName")
public class SkuItem implements Serializable {

@Id
private String _id;

String title;
//Getter and Setter of _id and title
}

但它总是选择集合名称为serializable(在ICentroRepository中扩展T的接口/类,而不是在@Document集合中定义的值 . 有人可以帮我设计我的通用mongo存储库吗?很多人提前谢谢你 . :): )

3 回答

  • 0

    你应该在 SkuItem 上创建一个 SkuItemRepository ,这就是Spring Data的工作方式,否则如果你调用 findById(id) 和......它会返回什么?

    当Spring的基本存储库接口只是通用的时,创建通用存储库有什么好处 . 所以你只是创建一个没有任何用处的标记界面 .

  • 0
    public interface ICentroRepository<T implements Serializable, ID implements 
    Serializable> extends MongoRepository<T, ID> {  
    }
    

    我不确定是不是?

  • -1
    public interface ICentroRepository<T extends Serializable, ID extends 
    Serializable> extends MongoRepository<T, ID> {  
    }
    

    public interface ICentroRepository extends MongoRepository<SkuItem, ID> {  
    }
    

相关问题