I need to implement filtering to all entities' repositories.
The filtering follows the same scenario, differing only in Filter and Entity params.
So now I have my filter interface:
interface RepositoryFilter<Entity, Filter> {
fun findAllByFilter(filter: Filter, pageable: Pageable): PageImpl<Entity>
}
An abstract class which implements several methods used in findAllByFilter implementation:
abstract class AbstractRepositoryFilter<Entity, Filter>(
private val jdbcTemplate: NamedParameterJdbcTemplate
) : RepositoryFilter<Entity, Filter> {
override fun findAllByFilter(filter: Filter, pageable: Pageable): RestResponsePage<Entity> {
val sql = initSqlQuery() // to be implemented in concrete class
val params = mutableMapOf<String, Any>()
appendFilterParams(filter, sql, params) // to be implemented in concrete class
val total = countFiltered(sql, params) // is implemented in AbstractRepositoryFilter
val result = collectFiltered(sql, params) // to be implemented in concrete class
return RestResponsePage(result, pageable, total)
}
}
A concrete class, that implements functionality for concrete Entity (Event):
class RepositoryFilterImpl(
jdbcTemplate: NamedParameterJdbcTemplate
) : AbstractRepositoryFilter<Event, EventFilter>(jdbcTemplate) {
override fun initSqlQuery(): StringBuilder {
// implemention
}
override fun appendFilterParams(filter: EventFilter, sql: StringBuilder, params: MutableMap<String, Any>) {
// implemention
}
override fun collectFiltered(sql: StringBuilder, params: MutableMap<String, Any>): List<Event>
{
// implemention
}
}
The result repository:
@Repository
interface EventRepository : CrudRepository<Event, Long>, RepositoryFilter<Event, EventFilter>
However, I get the same mistake, trying to compile this:
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property 'filter' found for type 'Event'
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:94) ~[spring-data-commons-3.4.2.jar:3.4.2]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:455) ~[spring-data-commons-3.4.2.jar:3.4.2]
I want to avoid boilerplate code, that's why I tried to use an abstract class...
-
I was trying to follow an example provided in Spring's Custom Repository Implementations. There is a UserRepository (my EventRepository), which extends CustomizedSave interface (my RepositoryFilter). In turn, CustomizedSave is implemented by CustomizedSaveImpl (my RepositoryFilterImpl as i hope)Svetlana– Svetlana2025年04月09日 20:27:06 +00:00Commented Apr 9 at 20:27
1 Answer 1
I think you should check , you annotate "@EnableJpaRepositories" at configuration.
Comments
Explore related questions
See similar questions with these tags.