@beikov - The Entity Mapping for Packages and ProtectedItems are like as in the Question part (not yet as suggested by you in the comments) of this post. The mapping for ProtectedItems and StreamMapping is similar to above mapping.
The criteria query joins are like below and the Hibernate Version is 5.6.9
Root<StreamMappings> root = query.from(StreamMappings.class);
Join<StreamMappings, ProtectedItems> protectedItemsJoin = root.join("protectedItemsList", JoinType.LEFT);
Join<ProtectedItems, Packages> packagesJoin = protectedItemsJoin.join("packagesList", JoinType.LEFT);
Generated Query -
select
distinct cast(streammapp0_.stream_id as char) as col_0_0_,
streammapp0_.stream_name as col_1_0_,
cast(packages4_.package_id as char) as col_2_0_,
packages4_.package_name as col_3_0_,
streammapp0_.created_at as col_4_0_,
streammapp0_.last_modified_at as col_5_0_
from
stream_mappings streammapp0_
left outer join
jt_protected_items_stream_mappings protectedi1_
on streammapp0_.account_id=protectedi1_.ci_account_id
and streammapp0_.content_id_extension=protectedi1_.content_id_extension
left outer join
protected_items protectedi2_
on protectedi1_.pi_account_id=protectedi2_.account_id
and protectedi1_.protected_item_id=protectedi2_.protected_item_id
left outer join
jt_packages_protected_items packagesli3_
on protectedi2_.account_id=packagesli3_.pi_account_id
and protectedi2_.protected_item_id=packagesli3_.protected_item_id
left outer join
packages packages4_
on packagesli3_.p_account_id=packages4_.account_id
and packagesli3_.package_id=packages4_.package_id
where
streammapp0_.account_id=?
order by
cast(streammapp0_.stream_id as char) asc,
cast(packages4_.package_id as char) asc limit ?
The problem is in below data - line 1 should be elimated and using right join this is possible as the right join on streammapping happens at the last.
Xcain3x(streamId=4009, streamName=IPL-HD, packageId=null, packageName=null, createdAt=2022-06-19T00:00:00Z, lastModifiedAt=2022-06-19T00:00:00Z)
Xcain3x(streamId=4009, streamName=IPL-HD, packageId=2, packageName=Package-2, createdAt=2022-06-19T00:00:00Z, lastModifiedAt=2022-06-19T00:00:00Z)
Xcain3x(streamId=4009, streamName=IPL-HD, packageId=4, packageName=Package-4, createdAt=2022-06-19T00:00:00Z, lastModifiedAt=2022-06-19T00:00:00Z)