现在的位置: 首页 > 综合 > 正文

Castle ActiveRecord关系映射

2012年08月22日 ⁄ 综合 ⁄ 共 4153字 ⁄ 字号 评论关闭

1.一对多的关系

例如产品与品牌的关系,一种品牌可以有多个产品,一种产品对应一种品牌:

产品(Product):

 1namespace EasyNet.Model
 2{
 3    using System.Collections.Generic;
 4
 5    using NHibernate.Criterion;
 6
 7    using Castle.ActiveRecord;
 8    using Castle.ActiveRecord.Queries;
 9
10    using Core;
11
12    [ActiveRecord("site_product")]
13    public class Product : ActiveRecordBase<Product>
14    {
15        [PrimaryKey(PrimaryKeyType.Native)]
16        public int Id getset; }
17
18        [Property]
19        public string Name getset; }
20
21        [Property]
22        public string ShortDescription getset; }
23
24        [Property]
25        public string FullDescription getset; }
26
27        [Property]
28        public int DisplayOrder getset; }
29
30        [Property]
31        public long CreatedDatetime getset; }
32
33        [Property]
34        public long UpdatedDatetime getset; }
35
36        [Property]
37        public string PictureUrl getset; }
38
39        [BelongsTo("BrandId")]
40        public ProductBrand Brand getset; }
41
42
43        [HasAndBelongsToMany(Table = "site_productinproductcategory", ColumnRef = "CategoryId", ColumnKey = "ProductId")]
44        public IList<ProductCategory> Categories getset; }
45
46        public static void DeleteAllByIds(string ids)
47        {
48            Product.DeleteAll(string.Format("Id in ({0})", ids));
49        }

50
51        public static PagedResult<Product[]> FindByCategory(int categoryId, int start, int limit)
52        {
53            CountQuery countQuery = new CountQuery(typeof(Product));
54
55            countQuery.SetParameter("Id", categoryId);
56
57            countQuery.Query = "select count(*) from Product as product left join product.Categories as category where category.Id in(:Id)";
58
59            int total = (int)ExecuteQuery(countQuery);
60
61            if (total == 0)
62            {
63                return null;
64            }

65
66            SimpleQuery<Product> query = new SimpleQuery<Product>("select product from Product as product left join product.Categories as category where category.Id in(:Id) order by product.DisplayOrder desc, product.UpdatedDatetime desc");
67
68            query.SetParameter("Id", categoryId);
69            query.SetQueryRange(start, limit); 
70
71
72            Product[] products = query.Execute();
73
74            PagedResult<Product[]> result = new PagedResult<Product[]>();
75
76            result.Data = products;
77            result.Total = total;
78
79            return result;
80        }

81    }

82
83}

84

 

产品品牌(ProductBrand):

 1namespace EasyNet.Model
 2{
 3    using System.Collections.Generic;
 4
 5    using NHibernate.Criterion;
 6
 7    using Castle.ActiveRecord;
 8    using Castle.ActiveRecord.Queries;
 9
10    [ActiveRecord("site_productbrand")]
11    public class ProductBrand : ActiveRecordBase<ProductBrand>
12    {
13        [PrimaryKey(PrimaryKeyType.Native)]
14        public int Id getset; }
15
16        [Property]
17        public string Name getset; }
18
19        [Property]
20        public string LogoUrl getset; }
21
22        [Property]
23        public int DisplayOrder getset; }
24
25        [HasMany(ColumnKey = "BrandId")]
26        public IList<Product> Products getset; }
27    }

28}

29

2.多对多关系

例如用户与角色的关系,一个用户可以有多种角色,一种角色对应多个用户:

用户(User):

  1namespace EasyNet.Model
  2{
  3    using System.Collections.Generic;
  4    using System.Text;
  5
  6    using NHibernate.Criterion;
  7
  8    using Castle.ActiveRecord;
  9    using Castle.ActiveRecord.Queries;
 10
 11    using Core;
 12
 13    [ActiveRecord("site_user")]
 14    public class User : ActiveRecordBase<User>
 15    {
 16        [PrimaryKey(PrimaryKeyType.Native)]
 17        public int Id getset; }
 18
 19        [Property()]
 20        public string Username getset; }
 21
 22        [Property()]
 23        public string Password getset; }
 24
 25        [Property()]
 26        public string Name getset; }
 27
 28        [Property()]
 29        public bool IsApproved getset; }
 30
 31        [Property()]
 32        public int Gender getset; }
 33
 34        [Property()]
 35        public string Email getset; }
 36
 37        [Property()]
 38        public string Telephone getset; }
 39

抱歉!评论已关闭.