Q:

Hibernate HQL Projection Using Entities ManyToMany Definitions Issue

Problem: HQL query is not returning any results when I reference an entities collection field as part of the HQL statement. It works for one HQL projection for example like this:
select inc.categoryTypes as categoryTypes from IncidentEntity inc where (inc.id = :id105019)

The categoryTypes is one of the IncidentEntity classes fields (which is a collection defined as a ManyToMany join as seen below). This works fine, but the issue arises when I am attempting to reference another projection collection that is mapped as a ManyToMany join.
select inc.categoryTypes as categoryTypes, inc.consequences as consequences from IncidentEntity inc where (inc.id = :id105019)

As soon as I do it like this, I get an empty set. Which means the SQL query that hibernate generates isn't returning anything. I have verified this by executing the command within SQL Manager which returns no results.
Here is the IncidentEntity:
/**
 * Database entity for the 'incidents' table records.<br>
 * Entity domain object is {@link nz.co.doltech.ims.shared.domains.Incident}
 * @author Ben Dol
 * 
 */
@javax.persistence.Entity(name = "incidents")
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
public class IncidentEntity implements Entity {

    ...

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "incident_categorytype", joinColumns = { 
            @JoinColumn(name = "incident_id") }, 
        inverseJoinColumns = {
            @JoinColumn(name = "categorytype_id") 
    })
    private Set<CategoryTypeEntity> categoryTypes = new HashSet<CategoryTypeEntity>();

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "incident_consequence", joinColumns = { 
            @JoinColumn(name = "incident_id") }, 
        inverseJoinColumns = {
            @JoinColumn(name = "consequence_id") 
    })
    private Set<ConsequenceEntity> consequences = new HashSet<ConsequenceEntity>();

    ...

    public Set<CategoryTypeEntity> getCategoryTypes() {
        return categoryTypes;
    }
    public void setCategoryTypes(Set<CategoryTypeEntity> categoryTypes) {
        this.categoryTypes = categoryTypes;
    }

    public Set<ConsequenceEntity> getConsequences() {
        return consequences;
    }
    public void setConsequences(Set<ConsequenceEntity> consequences) {
        this.consequences = consequences;
    }

    ...
}

CategoryTypeEntity relationship definition:
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "categoryTypes")
private Set<IncidentEntity> incidents = new HashSet<IncidentEntity>();

ConsequenceEntity relationship definition:
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "consequences")
private Set<IncidentEntity> incidents = new HashSet<IncidentEntity>();

Data structure:

Using Hibernate 3.6.10
Maybe I have setup the definitions wrong, or I am missing a limitation with the HQL here, I'm not sure. Would appreciate any help that I could get here. Thanks!
Regards,
Ben

A:

You know that you are generating a Cartesian product with this query, right?
The query can be better visualized as:
select categoryTypes, consequences 
from IncidentEntity inc 
inner join inc.categoryTypes as categoryTypes
inner join inc.consequences as consequences
where (inc.id = :id105019)

Because you haven't specify an explicit join, an INNER JOIN is assumed not a LEFT JOIN. 
Let's assume there are categories for the specified incident. So this query will return the categories for this incident, which is what you also reported:
select categoryTypes
from IncidentEntity inc 
inner join inc.categoryTypes as categoryTypes
where (inc.id = :id105019)

But when there are no consequences, the INNER JOIN will return no result, so:
select categoryTypes, consequences 
from IncidentEntity inc 
inner join inc.consequences as consequences
where (inc.id = :id105019)

will not return anything, but then this can happen for your query too:
select categoryTypes, consequences 
from IncidentEntity inc 
inner join inc.categoryTypes as categoryTypes
inner join inc.consequences as consequences
where (inc.id = :id105019)

