[英]@Document(collection = “Test”) not working in MongoRepository - Spring data - mongodb
I am trying to create generic mongo repository using generics of java.我正在尝试使用 java 的泛型创建通用的 mongo 存储库。 I have defined repository as below.我已将存储库定义如下。
public interface ICentroRepository<T extends Serializable, ID extends
Serializable> extends MongoRepository<T, ID> {
}
and I am defining document entity as below我正在定义文档实体如下
@Document(collection = "mycollectionName")
public class SkuItem implements Serializable {
@Id
private String _id;
String title;
//Getter and Setter of _id and title
}
but it always picks collection name as serializable(a interface/class which extends T in ICentroRepository, NOT a value defined in collection of @Document. Can anyone please help me in designing my generic mongo repository ? Many Thank you in advance. :) :)但它总是将集合名称选择为可序列化(在 ICentroRepository 中扩展 T 的接口/类,而不是在 @Document 集合中定义的值。谁能帮我设计我的通用 mongo 存储库?非常感谢。:): )
public interface ICentroRepository<T extends Serializable, ID extends
Serializable> extends MongoRepository<T, ID> {
}
to到
public interface ICentroRepository extends MongoRepository<SkuItem, ID> {
}
You shall create a SkuItemRepository
which is generified on SkuItem
, that's how Spring Data work, otherwise what shall it return if you call its findById(id)
and ...?你应该创建一个SkuItemRepository
,它是在 SkuItem 上SkuItem
,这就是 Spring Data 的工作方式,否则如果你调用它的findById(id)
和 ...,它会返回什么?
And what is the benefit of creating a generic repository, when Spring's base repository interface is just generic.当 Spring 的基本存储库接口只是通用的时,创建通用存储库有什么好处。 So you are just creating a marker interface which is not of any use.所以你只是在创建一个没有任何用处的标记界面。
public interface ICentroRepository<T implements Serializable, ID implements
Serializable> extends MongoRepository<T, ID> {
}
I am not sure Is that right?我不确定是不是这样?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.