java - How do you use type arguments in a Spring Data custom method implementation? -
per spring data commons documentation, adding custom method implementation spring data repository quite simple:
interface userrepositorycustom { public void somecustommethod(user user); } class userrepositorycustomimpl implements userrepositorycustom { public void somecustommethod(user user) { // custom implementation } } public interface userrepository extends jparepository<user, long>, userrepositorycustom { }
however, can't figure out is, if want use type arguments? example:
interface searchablerepository<t> { public page<t> search(string query, pageable page); } class searchablerepositoryimpl<t> implements searchablerepository<t> { public page<t> search(string query, pageable page) { // right here, need class<t> of t can create // jpa query } } public interface userrepository extends jparepository<user, long>, searchablerepository<user> { } public interface newsrepository extends jparepository<article, long>, searchablerepository<article> { }
in implementation search
method, need know class<t>
of supplied type argument t
can create jpa query. don't want add custom behavior repositories, because don't want repositories searchable. want apply searchablerepository
interface select repositories.
so how can this? or can it?
you can add class searchablerepositoryimpl , in newsrepositoryimpl inject (or create) new searchablerepositoryimpl(article.class)
class searchablerepositoryimpl<t> implements searchablerepository<t> { private class<t> klass; public searchablerepositoryimpl(class<t> klazz){ this.klass = klazz; } public page<t> search(string query, pageable page) { // right here, need class<t> of t can create // jpa query } } class newsrepositoryimpl<article> implements newsrepository<article> { private searchablerepository<article> searchrepo = new searchablerepositoryimpl(article.class); public page<t> search(string query, pageable page){ // delegate return searchrepo.search(query, page); } }
maybe new searchablerepositoryimpl
not spring best pratice, show idea
Comments
Post a Comment