0

I have an entity called forumUser that has several attributes. I want to make custom query and get only the id as a forumUser and leave the rest of attributes so I later add it to different services and dtos. The issue is that when I return the custom query, it cannot be converted to my entity ForumUser.

Query:

   @Query("SELECT p.id FROM ForumUser p WHERE LOWER(p.username) = LOWER(:username) ")
ForumUser find(@Param("username") String username);

Error:

java.util.UUID cannot be cast to com.example.demo.Entity.ForumUser
Chefk5
  • 346
  • 5
  • 13
  • Is there a way to keep it forumUser? – Chefk5 Aug 13 '18 at 08:33
  • 2
    yes create a constructor which hold a p.id or return all the object p! – YCF_L Aug 13 '18 at 08:33
  • Can you give an example as answer – Chefk5 Aug 13 '18 at 08:34
  • try like this https://stackoverflow.com/questions/29082749/spring-data-jpa-map-the-native-query-result-to-non-entity-pojo so that map the result to the entity so while fetch you will get an exact entity like resultset mapping – Ashok Kumar N Aug 13 '18 at 08:39
  • You can check my answer here https://stackoverflow.com/questions/50677700/how-to-work-with-alias-in-jpql/50677919#50677919 I provide two solutions hope this can help you :) – YCF_L Aug 13 '18 at 08:50

0 Answers0