You should not implement the actual Spring data repository, instead you should declare a different user interface where you can put your own methods.
Say you have a MyRepository defined as
@Repository public interface MyRepository extends JpaRepository<Tuple, Long> {}
Now you want to add your own findTuplesByMyArg() , for which you need to create a repository user interface
public interface MyRepositoryCustom { List<Tuple> findTuplesByMyArg(String myArg); }
Then comes the implementation of the user interface
public class MyRepositoryImpl implements MyRepositoryCustom { @PersistenceContext private EntityManager em; @Override public List<Tuple> findTuplesByMyArg(String myArg) { JPAQuery query = new JPAQuery(em); ... } }
And we need to change the MyRepository , so it expands the user repository, therefore
@Repository public interface MyRepository extends JpaRepository<Tuple, Long>, MyRepositoryCustom {}
And you can easily access findTuplesByMyArg() by entering MyRepository , for example.
@Service public class MyService { @Autowired private MyRepository myRepository; public List<Tuple> retrieveTuples(String myArg) { return myRepository.findTuplesByMyArg(myArg); } }
Note that the names here are important (you need to have the Impl default postfix configs in the repo implementation).
You can find all the necessary information here.
vtor
source share