1

I'm having difficulties generate an object to provide me data based on two tables.

Table One

public class User {
    private String name;
//...
}

Table Two

public class Profile {
    @JoinColumn(name = "user_id")
    private User user;
}

Objective

I need an object to give me the name and number of per user profiles.

Something like (in JSON):

[
    {"name":"John", "profiles":1 },
    {"name":"Maria", "profiles":5 },
    {"name":"Victor", "profiles":3 }
]

The best solution to do this?

1 Answer 1

2

First, create a bean that will be used to generate your Json:

public class ABean {
    private String name;
    private Integer profiles;

    // getters and setters here
}

Then, your spring data jpa query would look like this:

SELECT new ABean(p.user.name, count(p.id)) FROM Profile p JOIN p.user GROUP BY p.user.name
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.