1

I am trying to retrieve OneToMany association in my spring boot project.When I am returning the JSON response from controller it is only getting like normal list of string instead of proper JSON. This result is a join result from a JPQL query,

I am adding the repository method here,

@Query("SELECT ur.userId , r.role FROM Roles r JOIN r.roleJoin ur")
List<Roles> findByRole();

And my controller has the code like the following,

@GetMapping("/check")
public List<Roles> check() {
    return repoObj.findByRole();
}

And getting response like this,

[[2,"A"],[649,"B"],[651,"C"],[653,"A"],[658,"A"],[3,"A"],[1,"B"],[670,"B"]]

It is seemd to be a list of object, But defaultly spring boot controller will return the data in JSON format. But I only getting like the following. Since I need to access the JSON from my front end Angular application.

Can anyone help me to clarify that to send response in proper JSON itself instead of just a list ?

4
  • Die you use @RestController annotation on your controller? Commented Aug 25, 2018 at 9:04
  • yes. I already have that annotation Commented Aug 25, 2018 at 9:04
  • how does your roles class look like? Commented Aug 25, 2018 at 9:06
  • @Simon - I updated in question Commented Aug 25, 2018 at 9:09

1 Answer 1

1

The problem is that the projection in your query does not match the return type.

You assume that you get objects of type Roles but in your query you only select ur.nuserId and r.srole_desc

Either change your query to:

@Query("SELECT r FROM Roles r JOIN r.roleUserMappingJoin ur")

but this would be the same as findAll()

or create a DTO that holds the data you want to return.

Read more about projection and Spring Data JPA here:

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections

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

5 Comments

So how i can retrieve limited number of rows according to any particular condition? Means that If I need only the user details having user id = 45 , Then there is no provision to mention in JPQL query ? DTO means?
DTO stands for Data Transfer Object and is a enterprise application pattern introduced by Martin Fowler: martinfowler.com/eaaCatalog/dataTransferObject.html
And you should definitely learn the JPA basics before using it. There are many books and the Java EE tutorial. Read this chapter: docs.oracle.com/javaee/7/tutorial/…
OK. Understood. So only through JPQL , particular column projection is impossible ?
no that's not what i meant. But if you do it like you did the return type is List of Object[] and that's why the JSON serialization doesn't produce the result you've excpected. So you need a class or interface as return type that can be serialized to proper JSON.

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.