[Java][Spring][JPA]カスタムリポジトリを任意の名前に変更する
Spring-boot+Spring Data Jpaでクエリを実装するにあたって、カスタムクエリを実装したい場合にカスタムリポジトリを定義する場合がある
1 2 3 |
public interface EmpRepository extends JpaRepository<Emp, Integer>, EmpRepositoryCustom { } |
1 2 3 |
public interface EmpRepositoryCustom { List<Emp> getEmpList(Integer id, String name); } |
1 2 3 4 5 6 7 |
public class EmpRepositoryImpl implements EmpRepositoryCustom { @Override public List<Emp> getEmpList(Integer id, String name) { // クエリの実装 } } |
基本はこんな形。
が、「HogeRespositoryCustomをimplementしてるのがHogeRespositoryImplというのがイミフ」とレビュアーに突っ込まれたので、任意の名称に変更した。
1 2 3 4 5 6 7 |
public class EmpRepositoryCustomImpl implements EmpRepositoryCustom { @Override public List<Emp> getEmpList(Integer id, String name) { // クエリの実装 } } |
ただし、このままではSpring-boot起動時にSpring DATA JPAがカスタムリポジトリの実装クラスを見つけられないようで、
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property getEmpList found for type Emp!
Repositoryの設定ファイルにカスタムリポジトリの実装クラスを見つけるためにrepositoryImplementationPostfixを指定してsuffixをつけ加える必要がある模様。
1 |
@EnableJpaRepositories(basePackageClasses = { EmpRepository.class }, repositoryImplementationPostfix = "CustomImpl") |
最近のコメント