Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

c# - NHibernate: Select item with entry in element bag

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...