Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have had a case like yours. I had Configuration class which can be either Global Configuration, Project Specific Configuration, or User Specific Configuration. What I did was this:</p> <ol> <li>I map the Parent like I normally would in any case to all possible parents (except global, it means applied to all so no parents)</li> </ol> <blockquote> <pre><code>&lt; class name="ConfigurationDomain" table="configuration"&gt; &lt; property name="ProjectId" column="project_id" type="int" insert="false" update="false" /&gt; &lt; property name="UserId" column="user_id" type="int" insert="false" update="false" /&gt; &lt; many-to-one name="Project" column="project_id" lazy="false" /&gt; &lt; many-to-one name="User" column="estimator_id" lazy="false" /&gt; &lt; /class&gt; </code></pre> </blockquote> <ol start="2"> <li>I map the configuration as collection in every possible parents</li> </ol> <blockquote> <pre><code>&lt; class name="UserDomain" table="user"&gt; &lt; set name="ConfigurationList" lazy="true" cascade="all-delete-orphan"&gt; &lt; key column="user_id" /&gt; &lt; one-to-many class="ConfigurationDomain" /&gt; &lt; /set&gt; &lt; /class&gt; &lt; class name="ProjectDomain" table="user"&gt; &lt; set name="ConfigurationList" lazy="true" cascade="all-delete-orphan"&gt; &lt; key column="project_id" /&gt; &lt; one-to-many class="ConfigurationDomain" /&gt; &lt; /set&gt; &lt; /class&gt; </code></pre> </blockquote> <p>Just like that and it worked for me. How do I access configuration of, say, a user with id 55 is this:</p> <p>I don't use someUser.ConfigurationList since it's slow. I only map the parent so that I can do this in HQL (it's much faster):</p> <pre><code>select c from ConfigurationDomain c where c.UserId=55 </code></pre> <p>And to get Global Configuration I would do this:</p> <pre><code>select c from ConfigurationDomain c where (c.UserId IS NULL) and (c.ProjectId IS NULL) </code></pre> <p>On reflection, I think you can even remove the collection mapping if you decide to use HQL.</p> <p>NOTE: I've used Criteria in my early days with NHibernate, but then I found HQL is somewhat more powerful for me, other might have different opinion in this.</p>
    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.
    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