Castle ActiveRecord关系映射
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
40 [Property()]
41 public string Mobile { get; set; }
42
43 [Property(Default = "0")]
44 public int Birthday { get; set; }
45
46 [Property(Default = "0")]
47 public int Age { get; set; }
48
49 [Property(Default = "")]
50 public string Company { get; set; }
51
52 [Property(Default = "")]
53 public string Address { get; set; }
54
55 [Property(Default = "")]
56 public string Website { get; set; }
57
58 [Property()]
59 public long CreatedDatetime { get; set; }
60
61 [Property()]
62 public long UpdatedDatetime { get; set; }
63
64 [HasAndBelongsToMany(Table = "site_userinrole", ColumnRef = "RoleId", ColumnKey = "UserId")]
65 public IList<Role> Roles { get; set; }
66
67 /// <summary>
68 ///
69 /// </summary>
70 /// <param name="username"></param>
71 /// <returns></returns>
72 public static User FindByUsername(string username)
73 {
74 return FindOne(Expression.Eq("Username", username));
75 }
76
77 public static PagedResult<User[]> FindAll(int type, string condition, int start, int limit)
78 {
79 CountQuery countQuery = new CountQuery(typeof(User));
80
81 StringBuilder sbCountQuery = new StringBuilder();
82 StringBuilder sbQuery = new StringBuilder();
83
84 sbCountQuery.Append("select count(*) from User as user");
85 sbQuery.Append("from User as user");
86
87 if (type == 0)
88 {
89 sbCountQuery.AppendFormat(" where user.Name like '%{0}%' or user.Email like '%{0}%'", condition);
90 sbQuery.AppendFormat(" where user.Name like '%{0}%' or user.Email like '%{0}%'", condition);
91
92 }
93 else if (type == 1)
94 {
95 sbCountQuery.AppendFormat(" where user.Name like '%{0}%'", condition);
96 sbQuery.AppendFormat(" where user.Name like '%{0}%'", condition);
97 }
98 else if (type == 2)
99 {
100 sbCountQuery.AppendFormat(" where user.Email like '%{0}%'", condition);
101 sbQuery.AppendFormat(" where user.Email like '%{0}%'", condition);
102 }
103
104 countQuery.Query = sbCountQuery.ToString(); ;
105
106 int total = (int)ExecuteQuery(countQuery);
107
108 if (total == 0)
109 {
110 return null;
111 }
112
113 SimpleQuery<User> query = new SimpleQuery<User>(sbQuery.ToString());
114
115 User[] users = query.Execute();
116
117 PagedResult<User[]> result = new PagedResult<User[]>();
118
119 result.Data = users;
120 result.Total = total;
121
122 return result;
123 }
124 }
125}
126
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
40 [Property()]
41 public string Mobile { get; set; }
42
43 [Property(Default = "0")]
44 public int Birthday { get; set; }
45
46 [Property(Default = "0")]
47 public int Age { get; set; }
48
49 [Property(Default = "")]
50 public string Company { get; set; }
51
52 [Property(Default = "")]
53 public string Address { get; set; }
54
55 [Property(Default = "")]
56 public string Website { get; set; }
57
58 [Property()]
59 public long CreatedDatetime { get; set; }
60
61 [Property()]
62 public long UpdatedDatetime { get; set; }
63
64 [HasAndBelongsToMany(Table = "site_userinrole", ColumnRef = "RoleId", ColumnKey = "UserId")]
65 public IList<Role> Roles { get; set; }
66
67 /// <summary>
68 ///
69 /// </summary>
70 /// <param name="username"></param>
71 /// <returns></returns>
72 public static User FindByUsername(string username)
73 {
74 return FindOne(Expression.Eq("Username", username));
75 }
76
77 public static PagedResult<User[]> FindAll(int type, string condition, int start, int limit)
78 {
79 CountQuery countQuery = new CountQuery(typeof(User));
80
81 StringBuilder sbCountQuery = new StringBuilder();
82 StringBuilder sbQuery = new StringBuilder();
83
84 sbCountQuery.Append("select count(*) from User as user");
85 sbQuery.Append("from User as user");
86
87 if (type == 0)
88 {
89 sbCountQuery.AppendFormat(" where user.Name like '%{0}%' or user.Email like '%{0}%'", condition);
90 sbQuery.AppendFormat(" where user.Name like '%{0}%' or user.Email like '%{0}%'", condition);
91
92 }
93 else if (type == 1)
94 {
95 sbCountQuery.AppendFormat(" where user.Name like '%{0}%'", condition);
96 sbQuery.AppendFormat(" where user.Name like '%{0}%'", condition);
97 }
98 else if (type == 2)
99 {
100 sbCountQuery.AppendFormat(" where user.Email like '%{0}%'", condition);
101 sbQuery.AppendFormat(" where user.Email like '%{0}%'", condition);
102 }
103
104 countQuery.Query = sbCountQuery.ToString(); ;
105
106 int total = (int)ExecuteQuery(countQuery);
107
108 if (total == 0)
109 {
110 return null;
111 }
112
113 SimpleQuery<User> query = new SimpleQuery<User>(sbQuery.ToString());
114
115 User[] users = query.Execute();
116
117 PagedResult<User[]> result = new PagedResult<User[]>();
118
119 result.Data = users;
120 result.Total = total;
121
122 return result;
123 }
124 }
125}
126
角色(Role):
1namespace EasyNet.Model
2{
3 using System.Collections.Generic;
4
5 using Castle.ActiveRecord;
6
7 [ActiveRecord("site_role")]
8 public class Role : ActiveRecordBase<Role>
9 {
10 [PrimaryKey(PrimaryKeyType.Native)]
11 public int Id { get; set; }
12
13 [Property()]
14 public string Name { get; set; }
15
17 [HasAndBelongsToMany(Table="site_userinrole", ColumnRef="UserId", ColumnKey="RoleId")]
18 public IList<User> Users { get; set; }
19 }
20}
21
2{
3 using System.Collections.Generic;
4
5 using Castle.ActiveRecord;
6
7 [ActiveRecord("site_role")]
8 public class Role : ActiveRecordBase<Role>
9 {
10 [PrimaryKey(PrimaryKeyType.Native)]
11 public int Id { get; set; }
12
13 [Property()]
14 public string Name { get; set; }
15
17 [HasAndBelongsToMany(Table="site_userinrole", ColumnRef="UserId", ColumnKey="RoleId")]
18 public IList<User> Users { get; set; }
19 }
20}
21