2015-09-22 77 views
2

我在我的项目中使用了Spring Data,并且我有大量的存储库。现在,我想一个方法添加到一些仓库的,但不是所有的人,所以我创建了一个接口“LoggingRepositoryCustom”,即(简体)看起来是这样的:在Spring数据中自定义实现中间存储库

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

正如我需要有一个这个自定义实现,我也创建了“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 
     } 
} 

最后,我有一些库,应该拥有上述functionity,如“AppUserRepo”:

@Repository 
public interface AppUserRepo extends PagingAndSortingRepository<AppUser, Long>, LoggingRepositoryCustom<AppUser, Long> { 
     //methods of this repo 
} 

然而,当我尝试部署该应用程序,我得到以下异常:

org.springframework.data.mapping.PropertyReferenceException: No property save found for type AppUser! 

看来,自定义实现不会反映春季数据试图创建一个神奇的方法从名称约定,从而寻找属性“保存”的“AppUser”,这是不存在的。有没有一种方法可以实现一个接口,并由其他接口进一步扩展?

回答

1

我添加了同样的问题,在我的项目之一,另外,我一样遵循得到它的工作:

1 - 创建“父”接口和实现:

库:

@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 - 创建您的具体接口和实现:

库:

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

库定制:

public interface AppUserRepoCustom<AppUser, Long> { 
} 

仓库实现:

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

希望这有助于

+0

好吧,当我实现AppUserRepo和LoggingRepo继承的方法工作的,但我不希望创建的每一个存储库将扩大LoggingRepo的实现,因为实现是相同的所有的类... –

+0

您需要创建一个“扩展LoggingRepositoryImpl”(在该父级定制仓库中实现的常用逻辑)的每个单一仓库的“空”自定义实现,以便告诉spring数据它应该生成这些定制代码... – Pras

+0

嗯,是的,这或多或少是我的问题的解决方案。但是,如果可以避免创建所有存储库的空白实现,那么我会实现这一点,这将会更好:)谢谢您的回复! –

相关问题