1

Ok, I'm rather new to Spring Boot and I'm currently facing a problem with attempting to successfully run the following Query towards my MySQL-Database in Postman without receiving a list of null-values:

 @Query("SELECT user.id, user.email FROM ProjectUser user WHERE user.lastLogin IS NOT NULL AND user.userType = 'Test'") List<ProjectUser> test1(); 

User.id and user.email is of type bigint(20) and VARCHAR(255) in the database, and I suspect that it might be related to them not being declared as NVARCHAR, but I have not tried modifying this as I'm scared it will corrupt the database :P. However, I see that the conditioning works as the number of objects returned is correct, so I'm suspecting that the issue is mainly related to representing the values.

In comparison, the following NativeQuery works and returns non-null values in Postman:

 @Query(value = "SELECT id, email FROM user WHERE last_login IS NOT NULL AND user_type = 'Test'", nativeQuery = true) List<ProjectUser> test1(); 

I was just wondering if anyone here might have an idea to how to fix this problem? I don't necessarily see why I should not go with the NativeQuery-solution when it works, but it is more of curiosity and understanding the problem.

2
  • 1
    Try to use the following query @Query("SELECT user FROM ProjectUser user WHERE user.lastLogin IS NOT NULL AND user.userType = 'Test'") Commented Dec 18, 2020 at 13:40
  • Looks like it worked! Crazy. Would you mind explain briefly what the big error I'm making is? Thank you so much! Commented Dec 18, 2020 at 14:19

1 Answer 1

2

You should strictly distinguish hql/jpql query and native sql query.

When you use explicit list of columns in jpql select clause that means you should expect result as List<Object[]>.

So, for your case, you should correct the query in the following way:

@Query("SELECT user FROM ProjectUser user WHERE user.lastLogin IS NOT NULL AND user.userType = 'Test'") List<ProjectUser> test1(); 

For more detailed explanation about values you can use in SELECT clause see the documentation.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.