Problem:
I want to fetch list of records with provided limit at JPA
query/query Method level (using MariaDb
).
What I am trying or have tried:
I have been trying to get List
of POJO
. I used JPA query @Query
with LIMIT 1
, but It's not accepting the limit. It gives compile time error. So I tried PageRequest.of(0,1)
to to pass the limit with query, however, still doesn't work. I also tried with JPA query methods findTopByClientID
and findFirstByClientID
. No luck! I get the same exception. I am not sure if we need to add something in the configuration to support all this but this is RuntimeException
.
Exception that I'm getting:
Here is the exception cause in Stack trace. But I don't see any SQL
syntax error (created at runtime).
java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near at line 1
Code:
public interface UserDAO extends JpaRepository<UserPO, Integer>, JpaSpecificationExecutor<UserPO> {
Page<UserPO> findByClientID(Integer clientID, Pageable pr);
default Page<UserPO> getFromQueue() {
return findByClientID(124, PageRequest.of(0, 1));
}
}
@Service
class UserImpl {
public User getUser() {
final Page<UserPO> user = userDAO.getFromQueue();
}
}
I don't want to use Native Query
, using so I would loose advantage of using JPA. Also I'm using MariaDB
Database. Let me know If you need more detail or the question is not framed so well.