Spring Data中中间存储库的自定义实现 [英] Custom implementation of intermediate repository in Spring Data

查看:21
本文介绍了Spring Data中中间存储库的自定义实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用 Spring Data,并且我有很多存储库.现在我想向一些存储库添加一个方法,但不是所有存储库,所以我创建了一个接口 LoggingRepositoryCustom,它(简化)如下所示:

I am using Spring Data in my project and I have plenty of repositories. 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:

@NoRepositoryBean
public interface LoggingRepositoryCustom<T extends IEntity, ID extends Serializable> {
     <S extends T> S save(S entity, AppUser author);
}

由于我需要对此进行自定义实现,因此我还创建了实现此接口的 LoggingRepositoryImpl:

As I need to have a custom implementation of this, I have created also LoggingRepositoryImpl, that implements this interface:

@NoRepositoryBean
public class LoggingRepositoryImpl<T extends IEntity, ID extends Serializable> implements LoggingRepository {
      @Override
      public <S extends T> S save(S entity, AppUser author) {
           //impl
      }
}

最后,我有一些存储库,应该具有上述功能,例如AppUserRepo:

Lastly, I have some repositories, that should have the functionity above, e.g. 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!

好像自定义的实现没有体现出来,Spring Data试图从命名约定中创建一个神奇的方法,从而寻找AppUser的属性save",该属性不存在.有没有办法实现一个接口,由其他接口进一步扩展?

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. 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 - 创建您的父"接口和实现:

1 - create your "parent" interfaces and implementations :

存储库:

@NoRepositoryBean
public interface LoggingRepository<T extends IEntity, ID extends Serializable> extends PagingAndSortingRepository<T, Long>, LoggingRepositoryCustom<T, ID> {
}

仓库自定义

@Transactional(readOnly = true)
public interface LoggingRepositoryCustom<T extends IEntity, ID extends Serializable> {
     <S extends T> S save(S entity, AppUser author);
}

存储库自定义的实现:

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 - 创建您的特定接口和实现:

2 - Create your specific interfaces and implementations :

存储库:

@Repository
public interface AppUserRepo extends LoggingRepository<AppUser, Long>, AppUserRepoCustom {
}

存储库自定义:

public interface AppUserRepoCustom<AppUser, Long> {
}

存储库实现:

public class AppUserRepoImpl extends LoggingRepositoryImpl<AppUser, Long> implements AppUserRepoCustom {
}

希望能帮到你

这篇关于Spring Data中中间存储库的自定义实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆