I have a class User defined as
@Entity
@Table(name = "users")
@JsonIdentityInfo(generator = ObjectIdGenerators.UUIDGenerator.class, property = "jsonUUID")
public class User implements Serializable, UserDetails
{
    private static final long serialVersionUID = -7035881497059422985L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long                 id;
    protected String               firstname;
    protected String               lastname;
    protected String               username;
    protected ProfessionalCategory professional;
    protected String               companyName;
    @Email
    protected String               email;
    protected String               password;
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable
        (
            name = "role_user",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id")
        )
    protected Set<Role>            roles;
}
When I perform a GET request on /users/{id} I want Hibernate to fetch the value from all the fields in user and return the "full" object. But when I perform a GET request on /users/ I want to return a list of users containing only firstname and lastname. I do not just want Jackson to ignore certain fields during serialization, I also want Hibernate not to fetch data it does not need (because fetching all the roles for each user can be very costly since I use a join table).
I know I could write my own SQL queries, but then I would loose the benefits of using Hibernate. So is there an elegant way of solving this problem?
 
     
    