Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate is producing SQL with a bad join
    primarykey
    data
    text
    <p>I have an NHibernate Linq query which isn't working how I would expect.</p> <p>The problem seems to come from using a nullable int column from a left joined table in the where clause. This is causing the join to act like an inner join.</p> <pre><code>var list = this.WorkflowDiaryManager.WorkflowActionRepository.All .Fetch(x =&gt; x.CaseView) .Fetch(x =&gt; x.WorkflowActionType) .ThenFetchMany(x =&gt; x.WorkflowActionPriorityList) .Where(x =&gt; x.AssignedUser.Id == userId || x.CaseView.MooseUserId == userId) </code></pre> <p>The SQL produced by this looks like (from the join onwards - you don't need to see all the selects)</p> <pre><code>from Kctc.WorkflowAction workflowac0_ left outer join Kctc.WorkflowCaseView workflowca1_ on workflowac0_.CaseId=workflowca1_.CaseId left outer join Kctc.WorkflowActionType workflowac2_ on workflowac0_.WorkflowActionTypeId=workflowac2_.WorkflowActionTypeId left outer join Kctc.WorkflowActionPriority workflowac3_ on workflowac2_.WorkflowActionTypeId=workflowac3_.WorkflowActionTypeId ,Kctc.WorkflowCaseView workflowca4_ where workflowac0_.CaseId=workflowca4_.CaseId and (workflowac0_.AssignedUser=@p0 or workflowca4_.[MooseUserId]=@p1); @p0 = 1087 [Type: Int32 (0)], @p1 = 1087 [Type: Int32 (0)] </code></pre> <p>So the part that is causing the problem is line 5 of the snippet above. As you can see, NHibernate is trying to do an 'old-school' join on my WorkflowCaseView View. This causes the query to exclude otherwise valid actions which do not have a CaseId in the WorkflowAction table.</p> <p>Could anyone explain why NHibernate is writing this SQL, and how I might encourage it to produce a better query?</p> <p>Thanks!</p> <p>Important bits from WorkflowActionMap</p> <pre><code> Table("Kctc.WorkflowAction"); Id(x =&gt; x.Id).GeneratedBy.Identity().Column("WorkflowActionId"); References(x =&gt; x.WorkflowActionType).Column("WorkflowActionTypeId").Unique(); References(x =&gt; x.CompletedBy).Column("CompletedBy"); References(x =&gt; x.CaseView).Column("CaseId").Not.Update().Unique(); References(x =&gt; x.AssignedUser).Column("AssignedUser"); </code></pre> <p>Important bits from WorkflowCaseViewMap</p> <pre><code> Table("Kctc.WorkflowCaseView"); Id(x =&gt; x.Id).Column("CaseId"); Map(x =&gt; x.MooseUserId).Nullable(); </code></pre> <p>Looking at this, I wonder if I should have a HasMany going back the other way...</p> <p>EDIT. Doesn't seem to help</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.
 

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