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 { get; set; }
17
18 [Property]
19 public string Name { get; set; }
20
21 [Property]
22 public string ShortDescription { get; set; }
23
24 [Property]
25 public string FullDescription { get; set; }
26
27 [Property]
28 public int DisplayOrder { get; set; }
29
30 [Property]
31 public long CreatedDatetime { get; set; }
32
33 [Property]
34 public long UpdatedDatetime { get; set; }
35
36 [Property]
37 public string PictureUrl { get; set; }
38
39 [BelongsTo("BrandId")]
40 public ProductBrand Brand { get; set; }
41
42
43 [HasAndBelongsToMany(Table = "site_productinproductcategory", ColumnRef = "CategoryId", ColumnKey = "ProductId")]
44 public IList<ProductCategory> Categories { get; set; }
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
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 { get; set; }
17
18 [Property]
19 public string Name { get; set; }
20
21 [Property]
22 public string ShortDescription { get; set; }
23
24 [Property]
25 public string FullDescription { get; set; }
26
27 [Property]
28 public int DisplayOrder { get; set; }
29
30 [Property]
31 public long CreatedDatetime { get; set; }
32
33 [Property]
34 public long UpdatedDatetime { get; set; }
35
36 [Property]
37 public string PictureUrl { get; set; }
38
39 [BelongsTo("BrandId")]
40 public ProductBrand Brand { get; set; }
41
42
43 [HasAndBelongsToMany(Table = "site_productinproductcategory", ColumnRef = "CategoryId", ColumnKey = "ProductId")]
44 public IList<ProductCategory> Categories { get; set; }
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 { get; set; }
15
16 [Property]
17 public string Name { get; set; }
18
19 [Property]
20 public string LogoUrl { get; set; }
21
22 [Property]
23 public int DisplayOrder { get; set; }
24
25 [HasMany(ColumnKey = "BrandId")]
26 public IList<Product> Products { get; set; }
27 }
28}
29
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 { get; set; }
15
16 [Property]
17 public string Name { get; set; }
18
19 [Property]
20 public string LogoUrl { get; set; }
21
22 [Property]
23 public int DisplayOrder { get; set; }
24
25 [HasMany(ColumnKey = "BrandId")]
26 public IList<Product> Products { get; set; }
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 { get; set; }
18
19 [Property()]
20 public string Username { get; set; }
21
22 [Property()]
23 public string Password { get; set; }
24
25 [Property()]
26 public string Name { get; set; }
27
28 [Property()]
29 public bool IsApproved { get; set; }
30
31 [Property()]
32 public int Gender { get; set; }
33
34 [Property()]
35 public string Email { get; set; }
36
37 [Property()]
38 public string Telephone { get; set; }
39
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 { get; set; }
18
19 [Property()]
20 public string Username { get; set; }
21
22 [Property()]
23 public string Password { get; set; }
24
25 [Property()]
26 public string Name { get; set; }
27
28 [Property()]
29 public bool IsApproved { get; set; }
30
31 [Property()]
32 public int Gender { get; set; }
33
34 [Property()]
35 public string Email { get; set; }
36
37 [Property()]
38 public string Telephone { get; set; }
39