简体   繁体   中英

Query in Spring Boot returns null values

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM