1

All my repositories are extending commonService Repository which in return are extending JpaRepository and JpaSpecification

public interface CommonReadRepository<T>
        extends JpaRepository<T, Long>, JpaSpecificationExecutor<T> {

i want to define native Sql query in CommonReadRepository like : Mysql:select * from table limit 1,20;

@Query(value = "select * from ?1 limit ?2,?3", nativeQuery = true)

 List<?> customFindQuery(String tableName,int offset,int limit);

but i recieve SqlGrammerException and unfortunately , i did not find much about syntax in documentation.

i know if i can define the queries in repositories then i know the table name but is it possible to make it generic ?

Thanks

1
  • perhaps if you post the full stack trace then there would be more info to actually comment on. More like a MySQL exception ... Commented Jun 1, 2016 at 9:33

1 Answer 1

2

Base Repository Section defined in Spring data reference

follow the guidelines defined in the reference like the following :

@NoRepositoryBean
interface CommonReadRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {

  List<T> custonFindQuery();
}
@Repository
interface UserRepository extends CommonReadRepository<User, Long> {
  User findByEmailAddress(EmailAddress emailAddress);
}

For your specific query List<?> customFindQuery(String tableName,int offset,int limit);

its already support in JpaRepository by calling the method:

Page<T> findAll(Pageable pageable)

For example:

Page<User> all = userRepository .findAll(new PageRequest(3, 10));

where offest = 30 (3 x 10), and limit = 10

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.