0

I am trying to fetch data using native query method. I am able to fetch data using spring data JPA repository declared methods (findAll() etc) and using JPQL Queries.

  • When I am using Native query method , "select * from" is working. But when I am specifying "select username from " method is not working. Means When specifying column name, it is not working.

I am adding my code like this,

@Query(value = "select u.username from users u" , nativeQuery = true)
List<Users> findByUsername();

But the query using select * from users is working with no problem. Is this native query nature? Or is there any limited type of format the provider defines?

1 Answer 1

3

I think the problem is with your return variable. When you run "*select * from...*" query, you can return list of Users.

However, you want to fetch a column which is probably a varchar, so that you should return List of String:

@Query(value = "select u.username from users u" , nativeQuery = true)
List<String> findByUsername();
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.