Note that there are some explanatory texts on larger screens.

plurals
  1. POHQL join problem
    primarykey
    data
    text
    <p>I have no problem writing sql for the following problem. However, I would like to write it as HQL or ICriteria. I am using fluent nhibernate and the latest nhibernate. The situation:</p> <p>There are 6 classes A B C D AC AD. B inherits from A. AC represents a m:m relationship between A and C and AD represents a m:m relationship between A and D. Let us assume that all classes have an ID column. I would like to count the number of Cs and Ds B is associated with.</p> <p>There is no IList of, for example, Cs in A (and B for that matter). Still the classes are associated ...</p> <p>Here is some code (simplified):</p> <pre><code>public class A : Entity { } public class B : A { } public class C : Entity { } public class D : Entity { } public class AC : Entity { public virtual A A { get; set; } public virtual C C { get; set; } } public class AD : Entity { public virtual A A { get; set; } public virtual D D { get; set; } } </code></pre> <p>Is this possible to use HQL and ‘left join’ (to also show the Bs that have zero Cs and Ds) in my particular case?</p> <p>Thanks.</p> <p>Christian</p> <p>PS: </p> <p>I have played a bit with theta style joins but did not get the expected results and I don’t think ‘left joins’ are possible here aren’t they?</p> <p>PPS:</p> <p>This theta-style join kind of works but only if B is assciated with at least 1 C and D:</p> <pre><code>select B.Id, count(distinct AC.C.Id), count(distinct AD.D.Id) from AC AC, AD AD, B B where AC.A.Id = B.Id and AD.A.Id = B.Id group by B.Id </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload