I have tried every way to fetch only amenityMaster and entranceFacings as collections in projection without fetching rest of the fields. No solution on the net works. Looks like some limitation of hibenate query language but I am stuck
I only want to fetch amenityMaster and entranceFacings as collections without fetching houseNumber and address in a SINGLE QUERY. I have searched on the net for days and couldn't find an actual working way to do this.
Example query:- SELECT ia AS ia, ef AS ef FROM SmartdoorProperty sp LEFT JOIN sp.internalAmenities ia left JOIN sp.entanceFacings ef
The above query doesn't return their collection but a cartesian product. Please don't suggest JOIN FETCH. It throws MultipleBagFetchException and I dont want to fire multiple queries as I have multiple ManyToMany/OneToMany Fields in my actual code or select all unnecessary fields because JOIN FETCH doesn't allow fetching only some fields.
@Table(name = "smartdoor_property", schema = "smartdoor-master", indexes = { @Index(name = "city",columnList = "city"), @Index(name = "address",columnList = "address") , @Index(name = "zip_code",columnList = "zip_code") , @Index(name = "latitude",columnList = "latitude"), @Index(name = "longitude",columnList = "longitude") })
public class SmartdoorProperty extends AbstractEntity implements Serializable {
@Column(name = "house_number", length = 50)
@Column(name = "address", length = 255)
private String address;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "property_x_amenity", joinColumns = {
@JoinColumn(name = "property_id", referencedColumnName = "property_id") }, inverseJoinColumns = {
@JoinColumn(name = "amenity_id", referencedColumnName = "id") }, schema = "smartdoor-master")
@Fetch(FetchMode.SUBSELECT)
private List<AmenityMaster> amenityMaster;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "property_x_entrance_facing_master", joinColumns = {
@JoinColumn(name = "property_id", referencedColumnName = "property_id") }, inverseJoinColumns = {
@JoinColumn(name = "entrance_id", referencedColumnName = "entrance_id") }, schema = "smartdoor-master")
@Fetch(FetchMode.SUBSELECT)
private List<EntranceFacingMaster> entranceFacings;
}
The above query doesn't return their collection but a cartesian product. Please don't suggest JOIN FETCH. It throws MultipleBagFetchException and I dont want to fire multiple queries as I have multiple ManyToMany/OneToMany Fields in my actual code or select all unnecessary fields because JOIN FETCH doesn't allow fetching only some fields.