一对多关系通过外键关系连接两个表,而没有中间的表。

来源:互联网 发布:在淘宝网买东西可靠吗 编辑:程序博客网 时间:2024/06/04 20:03

一对多关系通过外键关系连接两个表,而没有中间的表。

首先先引用一段对集合的释义:

 

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 classCustomer

{

public virtualint Unid { get; set; }

public virtualFllName Name { get;set; }

public virtualDateTime CreateTime { get; set; }

public virtualstring Address { get;set; }

public virtualint Version { get;set; }

 

private IList<Call> _list = new List<Call>();

public virtualIList<Call> Phones

{

get { return _list; }

set { _list = value; }

}

}

 

public classFllName

{

public string FirstName {get; set; }

public string LastName {get; set; }

public string Names

{

       get

{

return (FirstName+"·"+LastName);

}

}

}

这个类我要用于component,和一对多关系应用,所以多了个FullName复合属性。

(二)Call

public classCall

{

public virtualint Unid { get; set; }

public virtualstring Phone { get;set; }

 

public virtualCustomer Customer { get;set; }

}

这个类就是onetomany中的many一方

(三)Customer mapping

<bagname="Phones"table="Calls"cascade="all"inverse="true">

      <keycolumn="CustomerId"></key>

      <one-to-manyclass="Domain.Entities.Call,Domain"/>

</bag>

(四)Call mapping

   <many-to-onename="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 callin cc.Phones)

    {

Console.WriteLine(call.Phone);

    }

}

其中的GetElementById方法就是查询一个Customer持久数据而已,但因为关系的建立,使得它的Phones属性得以填充。查看它的sql语句为:

SELECT customer0_.Unidas Unid0_0_,customer0_.Versionas Version0_0_,customer0_.FirstNameas FirstName0_0_,customer0_.LastNameas LastName0_0_,customer0_.CreateTimeas CreateTime0_0_,customer0_.Addressas Address0_0_ FROM Customer customer0_WHERE customer0_.Unid=@p0;@p0= 38

SELECT phones0_.CustomerIdas CustomerId1_,phones0_.Unidas Unid1_,phones0_.Unidas Unid1_0_,phones0_.Phoneas Phone1_0_,phones0_.CustomerIdas 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/

转载请注明:博客园

原创粉丝点击