NHibernate:Select 项在元素包中有条目
NHibernate: Select item with entry in element bag
我有一个 class 列表 属性。
public class Paperboy{
private int _id;
private string _lastname;
private string _firstname;
private string _mobile;
private string _phone;
private IList<string> _additionalPhoneNumbers;
}
列表被映射为带有键和元素的包。
<class name="Paperboy" table="tblPaperboy" lazy="false">
<id name="_id" column="Id" access="field" >
<generator class="assigned"/>
</id>
<property name ="_lastname" column="Lastname" access ="field" />
<property name ="_firstname" column="Firstname" access ="field" />
<property name ="_phone" column="Phone" access ="field" />
<property name ="_mobile" column="Mobile" access ="field" />
<bag name="_additionalPhoneNumbers" access="field" fetch="subselect" lazy="false" table="tblPaperboyAdditionalPhoneNumbers">
<key column="PaperboyId"/>
<element column="PhoneNumber" type="string"/>
</bag>
</class>
现在我尝试通过 phone 号码(_phone、_mobile 或 _additionalPhoneNumbers)来 select paberboys。 _phone 和 _mobile 没问题,但我受困于其他号码。
我尝试使用条件 api 并结束查询。
标准Api不知道属性AdditionalPhoneNumbers
ICriteria criteria = session.CreateCriteria(typeof(Paperboy));
criteria.CreateAlias("_additionalPhoneNumbers", "AdditionalPhoneNumbers");
Disjunction or = Restrictions.Disjunction();
or.Add(Restrictions.Eq("_phone", number));
or.Add(Restrictions.Eq("_mobile", number));
or.Add(Restrictions.Eq("AdditionalPhoneNumbers", number));
criteria.Add(or);
criteria.SetMaxResults(1);
return criteria.UniqueResult<Paperboy>();
查询结束不知道包含:
return query.Where(p => p.Mobile == number || p.Phone == number p.AdditionalPhoneNumbers.Contains(number)).Take(1).SingleOrDefault();
有人能帮帮我吗?
更新:
我被指向元素关键字。实施后我得到一个 SQL-Exception.
ICriteria criteria = session.CreateCriteria(typeof(Paperboy));
criteria.CreateAlias("_additionalPhoneNumbers", "APN");
Disjunction or = Restrictions.Disjunction();
or.Add(Restrictions.Eq("APN.elements", number));
criteria.Add(or);
criteria.SetMaxResults(1);
return criteria.UniqueResult<Paperboy>();
像这样的异常。
System.Data.SqlClient.SqlException: Der mehrteilige Bezeichner 'apn1_.PhoneNumber' konnte nicht gebunden werden.
问题是 属性 的 where 子句中没有使用 join 的别名。
apn1_(哪里)与 additiona4_(连接)。
已制作SQL:
[SELECT TOP (@p0) this_.Id as Id3_1_, this_.BusinessId as BusinessId3_1_, this_.AgencyKey as AgencyKey3_1_, this_.Lastname as Lastname3_1_, this_.Firstname as Firstname3_1_, this_.Title as Title3_1_, this_.Street as Street3_1_, this_.Zip as Zip3_1_, this_.City as City3_1_, this_.Phone as Phone3_1_, this_.Mobile as Mobile3_1_, this_.Comment as Comment3_1_, this_.StaffId as StaffId3_1_, this_.IsActive as IsActive3_1_, agency3_.Id as Id1_0_, agency3_.BusinessId as BusinessId1_0_, agency3_.RegionKey as RegionKey1_0_, agency3_.Shorttext as Shorttext1_0_, agency3_.Longtext as Longtext1_0_, agency3_.CompanyCodeId as CompanyC6_1_0_ FROM tblPaperboy this_ left outer join tblAgency agency3_ on this_.AgencyKey=agency3_.Id inner join tblPaperboyAdditionalPhoneNumbers additiona4_ on this_.Id=additiona4_.PaperboyId WHERE (apn1_.PhoneNumber = @p1)]
几乎与我刚刚对之前的 NHibernate 问题给出的答案相同:
基于此问答:NHibernate How do I query against an IList property?
我试图证明 (如文档中所述) 我们可以使用魔法词 ".elements"
:
17.1.4.1. Alias and property references
所以查询将触及您案例中的字符串元素
//or.Add(Restrictions.Eq("AdditionalPhoneNumbers", number));
or.Add(Restrictions.Eq("AdditionalPhoneNumbers.elements", number));
这将支持过滤IList<string>
似乎 NHibernate < 4 在创建连接时存在错误。
在 Radim Köhler and firo () 的帮助下,我最终使用了 HQL:
select
paperboy
from
Paperboy as paperboy
left join
paperboy._additionalPhoneNumbers number
where
paperboy._mobile = :nr or
paperboy._phone = :nr or
number = :nr
也可以通过使用 :nr in elements(paperboy._additionalPhoneNumbers)
来避免连接,但我无法替换附加号码中的某些字符。
我有一个 class 列表 属性。
public class Paperboy{
private int _id;
private string _lastname;
private string _firstname;
private string _mobile;
private string _phone;
private IList<string> _additionalPhoneNumbers;
}
列表被映射为带有键和元素的包。
<class name="Paperboy" table="tblPaperboy" lazy="false">
<id name="_id" column="Id" access="field" >
<generator class="assigned"/>
</id>
<property name ="_lastname" column="Lastname" access ="field" />
<property name ="_firstname" column="Firstname" access ="field" />
<property name ="_phone" column="Phone" access ="field" />
<property name ="_mobile" column="Mobile" access ="field" />
<bag name="_additionalPhoneNumbers" access="field" fetch="subselect" lazy="false" table="tblPaperboyAdditionalPhoneNumbers">
<key column="PaperboyId"/>
<element column="PhoneNumber" type="string"/>
</bag>
</class>
现在我尝试通过 phone 号码(_phone、_mobile 或 _additionalPhoneNumbers)来 select paberboys。 _phone 和 _mobile 没问题,但我受困于其他号码。
我尝试使用条件 api 并结束查询。
标准Api不知道属性AdditionalPhoneNumbers
ICriteria criteria = session.CreateCriteria(typeof(Paperboy));
criteria.CreateAlias("_additionalPhoneNumbers", "AdditionalPhoneNumbers");
Disjunction or = Restrictions.Disjunction();
or.Add(Restrictions.Eq("_phone", number));
or.Add(Restrictions.Eq("_mobile", number));
or.Add(Restrictions.Eq("AdditionalPhoneNumbers", number));
criteria.Add(or);
criteria.SetMaxResults(1);
return criteria.UniqueResult<Paperboy>();
查询结束不知道包含:
return query.Where(p => p.Mobile == number || p.Phone == number p.AdditionalPhoneNumbers.Contains(number)).Take(1).SingleOrDefault();
有人能帮帮我吗?
更新:
我被指向元素关键字。实施后我得到一个 SQL-Exception.
ICriteria criteria = session.CreateCriteria(typeof(Paperboy));
criteria.CreateAlias("_additionalPhoneNumbers", "APN");
Disjunction or = Restrictions.Disjunction();
or.Add(Restrictions.Eq("APN.elements", number));
criteria.Add(or);
criteria.SetMaxResults(1);
return criteria.UniqueResult<Paperboy>();
像这样的异常。
System.Data.SqlClient.SqlException: Der mehrteilige Bezeichner 'apn1_.PhoneNumber' konnte nicht gebunden werden.
问题是 属性 的 where 子句中没有使用 join 的别名。 apn1_(哪里)与 additiona4_(连接)。
已制作SQL:
[SELECT TOP (@p0) this_.Id as Id3_1_, this_.BusinessId as BusinessId3_1_, this_.AgencyKey as AgencyKey3_1_, this_.Lastname as Lastname3_1_, this_.Firstname as Firstname3_1_, this_.Title as Title3_1_, this_.Street as Street3_1_, this_.Zip as Zip3_1_, this_.City as City3_1_, this_.Phone as Phone3_1_, this_.Mobile as Mobile3_1_, this_.Comment as Comment3_1_, this_.StaffId as StaffId3_1_, this_.IsActive as IsActive3_1_, agency3_.Id as Id1_0_, agency3_.BusinessId as BusinessId1_0_, agency3_.RegionKey as RegionKey1_0_, agency3_.Shorttext as Shorttext1_0_, agency3_.Longtext as Longtext1_0_, agency3_.CompanyCodeId as CompanyC6_1_0_ FROM tblPaperboy this_ left outer join tblAgency agency3_ on this_.AgencyKey=agency3_.Id inner join tblPaperboyAdditionalPhoneNumbers additiona4_ on this_.Id=additiona4_.PaperboyId WHERE (apn1_.PhoneNumber = @p1)]
几乎与我刚刚对之前的 NHibernate 问题给出的答案相同:
基于此问答:NHibernate How do I query against an IList property?
我试图证明 (如文档中所述) 我们可以使用魔法词 ".elements"
:
17.1.4.1. Alias and property references
所以查询将触及您案例中的字符串元素
//or.Add(Restrictions.Eq("AdditionalPhoneNumbers", number));
or.Add(Restrictions.Eq("AdditionalPhoneNumbers.elements", number));
这将支持过滤IList<string>
似乎 NHibernate < 4 在创建连接时存在错误。
在 Radim Köhler and firo (
select
paperboy
from
Paperboy as paperboy
left join
paperboy._additionalPhoneNumbers number
where
paperboy._mobile = :nr or
paperboy._phone = :nr or
number = :nr
也可以通过使用 :nr in elements(paperboy._additionalPhoneNumbers)
来避免连接,但我无法替换附加号码中的某些字符。