Spring Data JPA: How can Query return Non- Entities Objects or List of Objects?

You can do something like

@NamedQuery(name="findWhatever", query="SELECT new path.to.dto.MyDto(e.id, e.otherProperty) FROM Student e WHERE e.id = ?1")

Then the MyDto object would just need a constructor defined with the correct fields i.e.

public MyDto(String id, String otherProperty) { this.id = id; this.otherProperty = otherProperty; }

Leave a Comment