I'm using jpa EntityManager with hibernate in my java spring application. Suppose I have a user entity like below:
public class User {
private Long id;
...
@ManyToOne
private Address address
}
And I have Custom user dto object for passing into client:
public class UserDTO {
private Long id;
private AddressDTO address;
...
}
And I have a UserRepository that exceute normal jpql query with EntityManager
and Query
.
Note, I need to have custom dto, because my dto has some fields that does not exist in entity and must be calculated in query. Now my question: is there any way with EntityManager that map flat query result into my nested UserDTO? In fact, I need to map result of address in AdressDTO inside UserDto and so on.
Note: I want to use jpql not native sql query.