Use column of join in where condition - nhibernate

I do the fallowing HQL with NHibernate:
from Contact a where IsInternal = 0
this give me the fallowing sql (from NHProfiler):
select TOP ( 25 /* #p0 */ ) contact0_.Id as Id29_,
contact0_.ObjectVersion as ObjectVe2_29_,
...
...
from Contact contact0_
left outer join Company contact0_1_
on contact0_.Id = contact0_1_.Id
left outer join Person contact0_2_
on contact0_.Id = contact0_2_.Id
left outer join Branch contact0_3_
on contact0_.Id = contact0_3_.Id
left outer join ContactGroup contact0_4_
on contact0_.Id = contact0_4_.Id
where contact0_.IsInternal = 0
I want now extend the where-condition with
... and (contact0_1.Id is not null or contact0_2_.Id is not null)
The question is now, how can I access the joined table in the HQL?
Best Regards, Thomas

Er, if the field is null, then referenced object is null. So:
where IsInternal = 0 and (a.person is not null or a.company is not null)

Related

Select statement where joins causing records to be excluded

I have the below query with multiple joins.The last 3 joins are required to get the g.fin_id value. This works fine (see results) BUT because some records in the ACCUM_ISS_CHAR_HIST table have e.char9_nme values of NULL, it excludes the records in the results altogether. So it seems as when the e.char9_nme value has a record then it will produce a result, but as soon as it has a Null value then it is excluded. I would still like to see the records even though the g.fin_id for those will then be blank because they have a e.char9_nme value of Null. How can I change the query to accomplish this?
select
a.acct_id,
c.fld3_txt,
b.issue_loc1_cde,
b.instr_id,
a.fld1_nme,
b.issue_cls2_nme,
g.fin_id,
e.char9_nme
from position_dg as a
inner join
infoportal..issue_dg as b on b.INSTR_ID = a.INSTR_ID
inner join
InfoPortal..IVW_ACCT as c on a.acct_id = c.acct_id
inner join
InfoPortal..DW_AcctCharDG as d on a.acct_id = d.acctid
inner join
ACCUM_ISS_CHAR_HIST as e on a.instr_id = e.instr_id
inner join
MD_FINANCIAL_ENTITY as f on e.char9_nme = f.fin_enty_name
inner join md_FINANCIAL_ENTITY_ALTERNATE_IDENTIFIER as g on
f.fin_enty_id = g.fin_enty_id
and b.MAT_EXP_DTE > getdate()
and b.issue_cls1_nme = 'Derivatives'
and a.as_of_tms >= getdate()-1
and b.iss_typ in ('FFX','IRS','EQF')
and d.AcctChrSetId = 'DerivativeRpt'
and d.EndTms IS NULL
and a.acct_id = 'FOGEMBLCR'
and g.id_ctxt_typ = 'LEGAL_ENTITY_IDENTIFIER'
and e.as_of_dte = (
select MAX (as_of_dte)-1
from accum_iss_char_hist
)
I expect the results to show fin_id records for some ond blank fin_id records for some, but at the moment only the ones with a fin_id record is hown and the rest is excluded from the results.
You are looking for a left join.
Join all those tables (last 3 as you said) as left join. For better clarity I have moved conditions of every tables in their ON clause and for base table a made a where clause.
select
a.acct_id,
c.fld3_txt,
b.issue_loc1_cde,
b.instr_id,
a.fld1_nme,
b.issue_cls2_nme,
g.fin_id,
e.char9_nme
from position_dg as a
inner join
infoportal..issue_dg as b on b.INSTR_ID = a.INSTR_ID
and b.MAT_EXP_DTE > getdate()
and b.issue_cls1_nme = 'Derivatives'
and b.iss_typ in ('FFX','IRS','EQF')
inner join
InfoPortal..IVW_ACCT as c on a.acct_id = c.acct_id
inner join
InfoPortal..DW_AcctCharDG as d on a.acct_id = d.acctid
and d.AcctChrSetId = 'DerivativeRpt'
and d.EndTms IS NULL
left join
ACCUM_ISS_CHAR_HIST as e on a.instr_id = e.instr_id
and e.as_of_dte = (
select MAX (as_of_dte)-1
from accum_iss_char_hist
)
left join
MD_FINANCIAL_ENTITY as f on e.char9_nme = f.fin_enty_name
left join
md_FINANCIAL_ENTITY_ALTERNATE_IDENTIFIER as g on f.fin_enty_id = g.fin_enty_id
and g.id_ctxt_typ = 'LEGAL_ENTITY_IDENTIFIER'
Where a.as_of_tms >= getdate()-1
and a.acct_id = 'FOGEMBLCR'

Use name of controllers to find table name in Select

I want to extract a ID , User_ID value from one of the Companies and Contract tables, depending on the ContorollerName value.
select P.TitleProject, P.StartDateProject, P.EndDateProject,P.ControllerID,P.RecordID,P.IsAllocated,P.ProjectStatus_ID,
CN.ControllerName,CN.PersianName,
PU.ProjectID,PU.UserID,PU.RoleID,
CASE
WHEN CN.ControllerName = 'Company' THEN
Companies.Id,Companies.[User_Id]
WHEN CN.ControllerName = 'Contract' THEN
Contracts.Id,Contracts.[User_Id]
END
from Projects P
left outer join Controllers CN ON P.ControllerID = CN.Id
left outer join ProjectUsers PU ON P.Id = PU.ProjectID
where P.IsAllocated = 1
For example, if ContorollerName is 'Company' , the select command is as follows :
select P.TitleProject, P.StartDateProject, P.EndDateProject,P.ControllerID,P.RecordID,P.IsAllocated,P.ProjectStatus_ID,
CN.ControllerName,CN.PersianName,
PU.ProjectID,PU.UserID,PU.RoleID,
Companies.Id,Companies.[User_Id]
You are on the right track -- using left join. But you need to add the tables to the from clause with the appropriate logic.
The logic for the join is quite unclear. The query looks something like this:
select . . .,
coalesce(c.id, co.id) as id,
coalesce(c.user_id, co.user_id) as user_id
from Projects P left join
Controllers CN
on P.ControllerID = CN.Id left join
ProjectUsers PU
on P.Id = PU.ProjectID left join
companies c
on c.? = ? and -- no idea what the right join conditions are
c.ControllerName = 'Company' left join
contracts co
on co.? = ? and -- no idea what the right join conditions are
co.ControllerName = 'Contract'
where P.IsAllocated = 1

Linq to Entities cycling left outer joins

I have following problem with Linq to entities.
Diagram:
Linq to entities query (incomplete yet):
from a in Anlaesse
join b in Beurteilung on a.AnlassID equals b.AnlassID into ab
from b in ab.DefaultIfEmpty()
join r in Rangliste on a.AnlassID equals r.AnlassId
join p in Pony on r.PonyId equals p.PonyID into rp
from p in rp.DefaultIfEmpty()
where a.AnlassID == 67
select new
{
BeurteilungId = b.BeurteilungID == null ? 0 : b.BeurteilungID,
PonyID = p.PonyID == null ? 0 : p.PonyID,
Name = p.Name,
PonyName1 = r.PonyName1,
AnlassId = a.AnlassID == null ? 0 : a.AnlassID
}
Generated SQL:
SELECT
[Extent1].[AnlassID] AS [AnlassID],
CASE WHEN ([Extent2].[BeurteilungID] IS NULL) THEN 0 ELSE
[Extent2].[BeurteilungID] END AS [C1],
CASE WHEN ([Extent4].[PonyID] IS NULL) THEN 0 ELSE [Extent4].[PonyID] END
AS [C2],
[Extent4].[Name] AS [Name],
[Extent3].[PonyName1] AS [PonyName1]
FROM [sspv].[Anlaesse] AS [Extent1]
LEFT OUTER JOIN [sspv].[Beurteilung] AS [Extent2] ON [Extent1].[AnlassID]
= [Extent2].[AnlassID]
INNER JOIN [sspv].[Rangliste] AS [Extent3] ON [Extent1].[AnlassID] =
[Extent3].[AnlassId]
LEFT OUTER JOIN [sspv].[Pony] AS [Extent4] ON [Extent3].[PonyId] =
[Extent4].[PonyID]
WHERE 67 = [Extent1].[AnlassID]
Problem is that I'm unable to add the left outer join between Pony and Beurteilung because all tables are already "used" in my query. Without this the result is wrong because it Returns for each Pony all Beurteilung.
Let's assume you want to left join Pony to Beurteilung. And let's assume both have a PonyId property. A clause like this would act as an inner join:
where p.PonyId == b.PonyId
A clause like this would act as a left join.
where b == null || p.PonyId == b.PonyId
However, you are finding this hard because it really doesn't look like you are using Entity Framework properly. Have you mapped any navigation properties or collections? If you haven't then add them because this is sort of the point of EF. If you have then you shouldn't have any need to use inner or left joins; you just traverse the navigation properties.

Why is this SQL query returning this result?

Can anyone tell me why this sql query is returning this result.
SELECT
M.MatterNumber_Id, M.MatterNumber, M.MatterName,
ISNULL(MP.Role_Cd, 'No Primary') AS PrimaryRole,
ISNULL(E.Name, 'No Primary') AS PrimaryName,
ISNULL(C.CommNumber, 'l.w#abc.ca;m.j#abc.com') AS Email
FROM
Matter M
LEFT OUTER JOIN
MatterPlayer MP ON M.MatterNumber_Id = MP.MatterNumber_Id AND
MP.Role_Cd IN ('Primary Lawyer', 'Primary Staff Member') AND
MP.EndDate IS NULL
LEFT OUTER JOIN
Entity E on MP.Entity_EID = E.Entity_EID
LEFT OUTER JOIN Communication C on MP.Entity_EID = C.Entity_EID AND
C.CommunicationType_Cd = 'Email'
LEFT OUTER JOIN
MatterExposure ME on M.MatterNumber_Id = ME.MatterNumber_Id AND
ME.AssessedDate > '7/10/2014' AND
ME.Currency_CD IS NOT NULL
WHERE
M.MatterStatus_Cd = 'Active' AND
ME.AssessedDate IS NULL AND
M.Matter_Cd in
(SELECT
rpl.Type_CD
FROM
RuleProfile_Tabs rpt
INNER JOIN
RuleProfile_LookupCode rpl ON rpt.RuleProfile_ID = rpl.RuleProfile_ID
WHERE
tab_id = 1034 AND Caption LIKE 'Reportable Matter%')
ORDER BY
Email, M.MatterName
But When i see the results I check one of the records returned and it should not have been returned.
One of the results returned:
Bailey, Richard
In the db i check the values in the tables it should not have been returned because Currency_CD is null and in the sql it states currency_cd is not null. Also the assessed date is AFTER 7/10/2014
Table Values:
MatterStatus_CD:
Active
AssessedDate:
7/24/2014
Currency_CD:
NULL
EndDate:
NULL
Where clauses are used for filtering, not the join clause. Every JOIN should have an ON. Here's a head start.
Select M.MatterNumber_Id, M.MatterNumber, M.MatterName,
IsNull(MP.Role_Cd, 'No Primary') as PrimaryRole,
IsNull(E.Name, 'No Primary') as PrimaryName,
IsNull(C.CommNumber, 'l.w#abc.ca;m.j#abc.com') as Email
From Matter M
Left Outer Join MatterPlayer MP on M.MatterNumber_Id = MP.MatterNumber_Id
Left Outer Join Entity E on MP.Entity_EID = E.Entity_EID
Left Outer Join Communication C on MP.Entity_EID = C.Entity_EID
Left Outer Join MatterExposure ME on M.MatterNumber_Id = ME.MatterNumber_Id
Where M.MatterStatus_Cd = 'Active'
AND ME.AssessedDate is Null
and M.Matter_Cd in (select rpl.Type_CD
from RuleProfile_Tabs rpt
inner join RuleProfile_LookupCode rpl on rpt.RuleProfile_ID = rpl.RuleProfile_ID
where tab_id = 1034 AND Caption like 'Reportable Matter%')
and MP.Role_Cd In ('Primary Lawyer', 'Primary Staff Member')
and MP.EndDate is Null
and ME.AssessedDate > '7/10/2014'
and ME.Currency_CD IS NOT NULL
and C.CommunicationType_Cd = 'Email'
Order by Email, M.MatterName
You are LEFT JOINing MatterExposure onto Matter. Your Me.Currency_CD IS NOT NULL criteria in the LEFT JOIN's ON statement is only saying, "don't join to records where Currency_CD is null..." but because this is a left join, none of your Matter table's records are lost in the join. So when you ask for Currency_CD you will get NULL.
To remove records from your query where Currency_CD is null in the MatterExposure table you will either need to specify Currency_CD IS NOT NULL in your where statement (instead of your join) or will need to use an INNER JOIN to your MatterExposure table.
It is because this join is a LEFT OUTER JOIN. If you want to enforce this join make it an inner join. Or move that check to the WHERE clause.

NHibernate Left Join Restriction

I have the following criteria.
session.CreateCriteria<DomainModels.Models.FreieAbrechnung>()
.CreateAlias("FreieAbrechnungPositionen", "fp", JoinType.LeftOuterJoin)
.Add(Restrictions.Eq("fp.IstAktiv", true))
.Add(Restrictions.Eq("Kunde.ID", kundenId))
.Add(Restrictions.Eq("IstAktiv", true))
.Add(Restrictions.Eq("Abgeschlossen", false))
.SetResultTransformer(CriteriaSpecification.DistinctRootEntity)
.List<DomainModels.Models.FreieAbrechnung>();
So I get this Sql-Statement:
SELECT this_.ID as ID13_1_, this_.Version as Version13_1_,
this_.KundeID as KundeID13_1_, this_.Erstellungsdatum as Erstellu4_13_1_,
this_.Druckdatum as Druckdatum13_1_, this_.Abgeschlossen as Abgeschl6_13_1_,
this_.AbgeschlossenDatum as Abgeschl7_13_1_, this_.IstAktiv as IstAktiv13_1_,
this_.ErstelltDurchID as Erstellt9_13_1_, this_.ErstelltAm as ErstelltAm13_1_,
this_.MutationDurch as Mutatio11_13_1_, this_.MutationAm as MutationAm13_1_,
fp1_.FreieAbrechnungID as FreieAbr3_3_, fp1_.ID as ID3_, fp1_.ID as ID12_0_,
fp1_.Version as Version12_0_, fp1_.FreieAbrechnungID as FreieAbr3_12_0_,
fp1_.KundeID as KundeID12_0_, fp1_.Bezeichnung as Bezeichn5_12_0_,
fp1_.Betrag as Betrag12_0_, fp1_.Erstellungsdatum as Erstellu7_12_0_,
fp1_.MwStSteuerCodeID as MwStSteu8_12_0_, fp1_.IstAktiv as IstAktiv12_0_,
fp1_.ErstelltDurchID as Erstell10_12_0_, fp1_.ErstelltAm as ErstelltAm12_0_,
fp1_.MutationDurch as Mutatio12_12_0_, fp1_.MutationAm as MutationAm12_0_
FROM
FreieAbrechnung this_
left outer join FreieAbrechnungPosition fp1_ on this_.ID=fp1_.FreieAbrechnungID
WHERE
fp1_.IstAktiv = 1 and this_.KundeID = 1 and this_.IstAktiv = 1 and this_.Abgeschlossen = 0
The problem is, that the restrictions fp1_.IstAktiv = 1 is in the where clause. This restrictions have to be in the left outer join. Like this:
left outer join FreieAbrechnungPosition fp1_ on this_.ID=fp1_.FreieAbrechnungID and fp1_.IstAktiv = 1
What should I change in the criteria, that I get the right Sql-Statement?
Thanks,
Dani
To add conditions to the join, you must use HQL instead of criteria.
A rough translation of your query would be:
select f
from FreieAbrechnung f
left join f.FreieAbrechnungPositionen fp with IstAktiv = 1
where f.IsAktiv = 1
... etc