[英]Custom implementation of intermediate repository in Spring Data
I am using Spring Data in my project and I have plenty of repositories.我在我的项目中使用 Spring Data 并且我有很多存储库。 Now I wanted to add a method to some of the repositories, but not all of them, so I have created an interface
LoggingRepositoryCustom
, that (simplified) looks like this:现在我想向一些存储库添加一个方法,但不是所有存储库,所以我创建了一个接口
LoggingRepositoryCustom
,它(简化)如下所示:
@NoRepositoryBean
public interface LoggingRepositoryCustom<T extends IEntity, ID extends Serializable> {
<S extends T> S save(S entity, AppUser author);
}
As I need to have a custom implementation of this, I have created also LoggingRepositoryImpl
, that implements this interface:由于我需要对此进行自定义实现,因此我还创建了实现此接口的
LoggingRepositoryImpl
:
@NoRepositoryBean
public class LoggingRepositoryImpl<T extends IEntity, ID extends Serializable> implements LoggingRepository {
@Override
public <S extends T> S save(S entity, AppUser author) {
//impl
}
}
Lastly, I have some repositories, that should have the functionity above, eg AppUserRepo
:最后,我有一些存储库,应该具有上述功能,例如
AppUserRepo
:
@Repository
public interface AppUserRepo extends PagingAndSortingRepository<AppUser, Long>, LoggingRepositoryCustom<AppUser, Long> {
//methods of this repo
}
However, when I try to deploy this application, I get the following exception:但是,当我尝试部署此应用程序时,出现以下异常:
org.springframework.data.mapping.PropertyReferenceException: No property save found for type AppUser!
It seems that the custom implementation is not reflected and Spring Data tries to create a magical method from the name convention, thus looking for property "save" of AppUser
, which does not exist.似乎自定义实现没有体现出来,Spring Data 试图从命名约定中创建一个神奇的方法,从而寻找
AppUser
属性“save”,该属性不存在。 Is there a way to implement an interface, that is further extended by other interfaces?有没有办法实现一个接口,由其他接口进一步扩展?
I add the same issue in one of my project ... and i did as follow to get it working :我在我的一个项目中添加了同样的问题......我做了以下工作以使其正常工作:
1 - create your "parent" interfaces and implementations : 1 - 创建您的“父”接口和实现:
Repository :存储库:
@NoRepositoryBean
public interface LoggingRepository<T extends IEntity, ID extends Serializable> extends PagingAndSortingRepository<T, Long>, LoggingRepositoryCustom<T, ID> {
}
Repository custom存储库自定义
@Transactional(readOnly = true)
public interface LoggingRepositoryCustom<T extends IEntity, ID extends Serializable> {
<S extends T> S save(S entity, AppUser author);
}
Implementation of the repository custom :存储库自定义的实现:
public class LoggingRepositoryImpl<T extends IEntity, ID extends Serializable> implements LoggingRepositoryCustom<T, ID> {
@Override
public <S extends T> S save(S entity, AppUser author) {
//impl
}
}
2 - Create your specific interfaces and implementations : 2 - 创建您的特定接口和实现:
repository :存储库:
@Repository
public interface AppUserRepo extends LoggingRepository<AppUser, Long>, AppUserRepoCustom {
}
repository custom :存储库自定义:
public interface AppUserRepoCustom<AppUser, Long> {
}
repository implementation :存储库实现:
public class AppUserRepoImpl extends LoggingRepositoryImpl<AppUser, Long> implements AppUserRepoCustom {
}
hope this helps希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.