NHibernate: Select item with entry in element bag

六眼飞鱼酱① 提交于 2019-11-27 08:36:41

问题


I have a class with property of list.

public class Paperboy{
     private int _id;
     private string _lastname;
     private string _firstname;
     private string _mobile;
     private string _phone;

     private IList<string> _additionalPhoneNumbers;
}

The List is mapped as bag with key and element.

<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>

Now I try to select paberboys by their phone number (_phone, _mobile or in _additionalPhoneNumbers). _phone and _mobile are no problem but I am stuck with the additional numbers.

I tried with criteria api and with query over.

Criteria Api does not know the property 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>();

Query Over does not know Contains:

 return query.Where(p => p.Mobile == number || p.Phone == number p.AdditionalPhoneNumbers.Contains(number)).Take(1).SingleOrDefault();

Could someone help me out?


UPDATE:

I was pointed to the elements-keyword. After implementing I get an 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>();

Exception like this.

System.Data.SqlClient.SqlException: Der mehrteilige Bezeichner 'apn1_.PhoneNumber' konnte nicht gebunden werden.

Problem is the alias for join is not used in the where clause for the property. apn1_ (where) vs. additiona4_ (join).

Produced 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)]


回答1:


Almost the same answer I just gave to previous NHibernate question: QueryOver IList<string> property

Based on this Q & A: NHibernate How do I query against an IList property?

Where I tried to show that (as mentioned in the documentation) we can use magical word ".elements":

17.1.4.1. Alias and property references

So the query which will touch the string elements in your case

//or.Add(Restrictions.Eq("AdditionalPhoneNumbers", number));
or.Add(Restrictions.Eq("AdditionalPhoneNumbers.elements", number));

And that will support filtering IList<string>




回答2:


Seems like NHibernate < 4 has a bug with creating the join. With the help of Radim Köhler and firo (see other Post) I ended up using HQL:

select
   paperboy
from
   Paperboy as paperboy 
left join 
   paperboy._additionalPhoneNumbers number
where
   paperboy._mobile = :nr or
   paperboy._phone = :nr or
   number = :nr

It is also possible to avoid the join by using :nr in elements(paperboy._additionalPhoneNumbers) but than I could not replace some characters in the additional number.



来源:https://stackoverflow.com/questions/27961444/nhibernate-select-item-with-entry-in-element-bag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!