一對多關系通過外鍵關系連接配接兩個表,而沒有中間的表。
首先先引用一段對集合的釋義:
Bag:對象集合,每個元素可以重複。例如{1,2,2,6,0,0},在.Net中相當于IList或者IList<T>實作。
Set:對象集合,每個元素必須唯一。例如{1,2,5,6},在.Net中相當于ISet或者ISet<T>實作,Iesi.Collections.dll程式集提供ISet集合。
List:整數索引對象集合,每個元素可以重複。例如{{1,"YJingLee"},{2,"CnBlogs"},{3,"LiYongJing"}},在.Net中相當于ArraryList或者List<T>實作。
Map:鍵值對集合。例如{{"YJingLee",5},{"CnBlogs",7},{"LiYongJing",6}},在.Net中相當于HashTable或者IDictionary<Tkey,TValue>實作。
持久類:
(一)Customer
public class Customer
{
public virtual int Unid { get; set; }
public virtual FllName Name { get; set; }
public virtual DateTime CreateTime { get; set; }
public virtual string Address { get; set; }
public virtual int Version { get; set; }
private IList<Call> _list = new List<Call>();
public virtual IList<Call> Phones
{
get { return _list; }
set { _list = value; }
}
public class FllName
public string FirstName { get; set; }
public string LastName { get; set; }
public string Names
get
return (FirstName+"·"+LastName);
}
這個類我要用于component,和一對多關系應用,是以多了個FullName複合屬性。
(二)Call
public class Call
public virtual string Phone { get; set; }
public virtual Customer Customer { get; set; }
這個類就是onetomany中的many一方
(三)Customer mapping
<bag name="Phones" table="Calls" cascade="all" inverse="true">
<key column="CustomerId"></key>
<one-to-many class="Domain.Entities.Call,Domain"/>
</bag>
(四)Call mapping
<many-to-one name="Customer" column="CustomerId"
class="Domain.Entities.Customer,Domain" not-null="true"/>
說明一下:
·對于資料庫表之間的關聯關系,nhibernate也是mapping過來了(自己了解),是以不必在資料庫中為資料表人為的建立關系,如果那樣的話,nhibernate中就不必建立一對多關系了(這個我還沒有測試)。
·對于一對多關系中的兩方:一的一方帶一個集合屬性,而這個集合是多的那一方的集合;而多的一方帶有一個一方的類型的屬性,可以這樣描述:一個父親帶有一群孩子,而一個孩子心中(現實也是)也有一個父親。而對于對象的nhb,資料庫中的外鍵由對象來描述。
其實在這裡,關系已經建立起來了,要做的就是進行curd了。
(一)查詢
[Test]
public void TestGetOne()
Customer cc = hh.GetElementById(38);
foreach (Call call in cc.Phones)
{
Console.WriteLine(call.Phone);
}
其中的GetElementById方法就是查詢一個Customer持久資料而已,但因為關系的建立,使得它的Phones屬性得以填充。檢視它的sql語句為:
SELECT customer0_.Unid as Unid0_0_, customer0_.Version as Version0_0_, customer0_.FirstName as FirstName0_0_, customer0_.LastName as LastName0_0_, customer0_.CreateTime as CreateTime0_0_, customer0_.Address as Address0_0_ FROM Customer customer0_ WHERE customer0_.Unid=@p0;@p0 = 38
SELECT phones0_.CustomerId as CustomerId1_, phones0_.Unid as Unid1_, phones0_.Unid as Unid1_0_, phones0_.Phone as Phone1_0_, phones0_.CustomerId as CustomerId1_0_ FROM Calls phones0_ WHERE phones0_.CustomerId=@p0;@p0 = 38
分别查詢兩個表中的資料
(二)添加
public void TestAdd()
Customer c = new Customer
Name = new FllName { FirstName = "張", LastName = "清" },
Address = "清河縣1"
};
Call phones = new Call { Phone = "7777778" };
Call phones1 = new Call { Phone = "9999978" };
phones.Customer = c;
phones1.Customer = c;
try
c.Phones.Add(phones);
c.Phones.Add(phones1);
catch
{ }
hh.AddUpdateDelete(Domain.Enums.eOperation.Add, c);
這裡的Customer的Phones是一個Call的集合。
(三)删除
public void TestDelete()
Customer c = new Customer {Unid=38};
hh.AddUpdateDelete(Domain.Enums.eOperation.Delete, c);
直接删除就行了。
(四)更新
public void TestUpdate()
Customer c;
c = hh.GetElementById(40);
c.Phones[0].Phone = "55555555";
hh.AddUpdateDelete(Domain.Enums.eOperation.Update, c);
部落格園大道至簡
http://www.cnblogs.com/jams742003/轉載請注明:部落格園