Assuming I have a class like:
public class Primary {
private int id;
private Secondary secondary;
private String otherMember1;
private float otherMember2;
etc...
}
I'm trying to create a DTO projection with a constructor in HQL with a query like:
SELECT com.example.Primary(p.id, p.secondary) from Primary p LEFT JOIN FETCH p.secondary
However, I get an error message when attempting to do that:
query specified join fetching, but the owner of the fetched association was not present in the select list
However, I feel like I am selecting it, it's the second argument of the constructor.
Most stack questions/answers I've seen tell me just to get rid of the FETCH
but the issue is that I actually need it. I need the secondary
object data and if I try to remove the FETCH
the data is not retrieved or is retrieved later with terrible performance. Additionally, if I simply mark p.secondary
as EAGER instead of LAZY in the relationship annotation the performance is atrocious as well.
I've also tried fetching through other means (Entity Graphs) to avoid using the FETCH
keyword and it actually results in the same error message.
Is there some way to do a DTO of some kind where I can JOIN FETCH
?