I am trying to create generic mongo repository using generics of java. 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. :) :)
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 ...?
And what is the benefit of creating a generic repository, when Spring's base repository interface is just generic. 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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.