I have the following two DTOs in My project,
public interface DeviceDTOMini {
String getName();
ClientDTOMini getClient();
}
public interface DeviceDTOFull {
String getName();
ClientDTOMini getClient();
DeviceModelDTOMini getDeviceModel();
}
below is the repository method to fetch data:
@EntityGraph( attributePaths = {"client","deviceModel"} )
<T> Page<T> findAllByClientIdAndDeletedAtIsNull( Long clientId, Pageable pageable, Class<T> type );
The above method works fine with DeviceDTOFull but it gives the following error when I use DeviceDTOMini:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=generatedAlias1,role=com.test.test.model.device.Device.client,tableName=clients,tableAlias=client1_,origin=devices device0_,columns={device0_.client_id ,className=com.test.test.model.client.Client}}] [select generatedAlias0.name, generatedAlias0.id, generatedAlias0.uuid from com.test.test.model.device.Device as generatedAlias0 left join generatedAlias0.client as generatedAlias1 where ( generatedAlias1.id=:param0 ) and ( generatedAlias0.deletedAt is null )]; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=generatedAlias1,role=com.test.test.model.device.Device.client,tableName=clients,tableAlias=client1_,origin=devices device0_,columns={device0_.client_id ,className=com.test.test.model.client.Client}}] [select generatedAlias0.name, generatedAlias0.id, generatedAlias0.uuid from com.test.test.model.device.Device as generatedAlias0 left join generatedAlias0.client as generatedAlias1 where ( generatedAlias1.id=:param0 ) and ( generatedAlias0.deletedAt is null )]
I've searched a lot and found some links but none of them provides the solution. Whereas only workaround is mentioned in all those.
Is there any solution to fetch only required fields from JPA?
https://jira.spring.io/browse/DATAJPA-684
Overriding Spring-Data-JPA default method annotating with EntityGraph causes QueryException