One To Many
the one is Blog:
the many is:
Blog
1using System;
2using System.Collections;
3
4using Castle.ActiveRecord;
5using Castle.ActiveRecord.Queries;
6using NHibernate.Expression;
7
8namespace CastleLearning.OneToMany
9{
10 [ActiveRecord]
11 public class Blog : ActiveRecordBase
12 {
13 private int id;
14 private String name;
15 private String author;
16 private IList posts = new ArrayList();
17
18 public Blog()
19 {
20 }
21
22 public Blog(String name)
23 {
24 this.name = name;
25 }
26
27 [PrimaryKey]
28 public int Id
29 {
30 get { return id; }
31 set { id = value; }
32 }
33
34 [Property]
35 public String Name
36 {
37 get { return name; }
38 set { name = value; }
39 }
40
41 [Property]
42 public String Author
43 {
44 get { return author; }
45 set { author = value; }
46 }
47
48 [HasMany(typeof(Post),
49 Table = "Posts", ColumnKey = "blogid",
50 Inverse = true, Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
51 public IList Posts
52 {
53 get { return posts; }
54 set { posts = value; }
55 }
56
57 public static void DeleteAll()
58 {
59 ActiveRecordBase.DeleteAll(typeof(Blog));
60 }
61
62 public static Blog[] FindAll()
63 {
64 return (Blog[])ActiveRecordBase.FindAll(typeof(Blog));
65 }
66
67 public static Blog Find(int id)
68 {
69 return (Blog)ActiveRecordBase.FindByPrimaryKey(typeof(Blog), id);
70 }
71
72 public static Blog[] FindByBlogName(string blogName)
73 {
74 SimpleQuery<Blog> q = new SimpleQuery<Blog>(typeof(Blog),@"from Blog b where b.Name = ?",blogName);
75 return q.Execute();
76 }
77
78 }
79}
80
1using System;
2using System.Collections;
3
4using Castle.ActiveRecord;
5using Castle.ActiveRecord.Queries;
6using NHibernate.Expression;
7
8namespace CastleLearning.OneToMany
9{
10 [ActiveRecord]
11 public class Blog : ActiveRecordBase
12 {
13 private int id;
14 private String name;
15 private String author;
16 private IList posts = new ArrayList();
17
18 public Blog()
19 {
20 }
21
22 public Blog(String name)
23 {
24 this.name = name;
25 }
26
27 [PrimaryKey]
28 public int Id
29 {
30 get { return id; }
31 set { id = value; }
32 }
33
34 [Property]
35 public String Name
36 {
37 get { return name; }
38 set { name = value; }
39 }
40
41 [Property]
42 public String Author
43 {
44 get { return author; }
45 set { author = value; }
46 }
47
48 [HasMany(typeof(Post),
49 Table = "Posts", ColumnKey = "blogid",
50 Inverse = true, Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
51 public IList Posts
52 {
53 get { return posts; }
54 set { posts = value; }
55 }
56
57 public static void DeleteAll()
58 {
59 ActiveRecordBase.DeleteAll(typeof(Blog));
60 }
61
62 public static Blog[] FindAll()
63 {
64 return (Blog[])ActiveRecordBase.FindAll(typeof(Blog));
65 }
66
67 public static Blog Find(int id)
68 {
69 return (Blog)ActiveRecordBase.FindByPrimaryKey(typeof(Blog), id);
70 }
71
72 public static Blog[] FindByBlogName(string blogName)
73 {
74 SimpleQuery<Blog> q = new SimpleQuery<Blog>(typeof(Blog),@"from Blog b where b.Name = ?",blogName);
75 return q.Execute();
76 }
77
78 }
79}
80
the many is:
Post
1using System;
2using System.Collections;
3using Castle.ActiveRecord;
4using NHibernate;
5
6namespace CastleLearning.OneToMany
7{
8
9 [ActiveRecord]
10 public class Post : ActiveRecordBase
11 {
12 private int id;
13 private String title;
14 private String contents;
15 private String category;
16 private DateTime created;
17 private bool published;
18 private Blog blog;
19
20 public Post()
21 {
22 created = DateTime.Now;
23 }
24
25 public Post(Blog blog, String title, String contents, String category)
26 : this()
27 {
28 this.blog = blog;
29 this.title = title;
30 this.contents = contents;
31 this.category = category;
32 }
33
34 [PrimaryKey]
35 public int Id
36 {
37 get { return id; }
38 set { id = value; }
39 }
40
41 [Property]
42 public String Title
43 {
44 get { return title; }
45 set { title = value; }
46 }
47
48 [Property(ColumnType = "StringClob")]
49 public String Contents
50 {
51 get { return contents; }
52 set { contents = value; }
53 }
54
55 [Property]
56 public String Category
57 {
58 get { return category; }
59 set { category = value; }
60 }
61
62 [BelongsTo("blogid")]
63 public Blog Blog
64 {
65 get { return blog; }
66 set { blog = value; }
67 }
68
69 [Property("created")]
70 public DateTime Created
71 {
72 get { return created; }
73 set { created = value; }
74 }
75
76 [Property("published")]
77 public bool Published
78 {
79 get { return published; }
80 set { published = value; }
81 }
82
83 public static void DeleteAll()
84 {
85 ActiveRecordBase.DeleteAll(typeof(Post));
86 }
87
88 public static Post[] FindAll()
89 {
90 return (Post[])ActiveRecordBase.FindAll(typeof(Post));
91 }
92
93 public static Post[] GetPostsFromTitle(string title)
94 {
95 MyPostCustomQuery q = new MyPostCustomQuery();
96 q.MaxResults = 3;
97 q.Title = title;
98 return (Post[])ExecuteQuery(q);
99 }
100
101 public static Post[] GetPostsFromAuthor(String author)
102 {
103 return (Post[])Execute(typeof(Blog),
104 delegate(ISession session, object instance)
105 {
106 // create the query
107 IQuery query = session.CreateQuery("from Post p where p.Blog.Author = :author");
108
109 // set the parameters
110 query.SetString("author", (String)instance);
111
112 // fetch the results
113 IList results = query.List();
114
115 // OPTIONAL: convert the results to an array or
116 // something meaningful, instead of returning the IList
117 Post[] posts = new Post[results.Count];
118 results.CopyTo(posts, 0);
119
120 // return
121 return posts;
122 }, author);
123 }
124 }
125}
126
1using System;
2using System.Collections;
3using Castle.ActiveRecord;
4using NHibernate;
5
6namespace CastleLearning.OneToMany
7{
8
9 [ActiveRecord]
10 public class Post : ActiveRecordBase
11 {
12 private int id;
13 private String title;
14 private String contents;
15 private String category;
16 private DateTime created;
17 private bool published;
18 private Blog blog;
19
20 public Post()
21 {
22 created = DateTime.Now;
23 }
24
25 public Post(Blog blog, String title, String contents, String category)
26 : this()
27 {
28 this.blog = blog;
29 this.title = title;
30 this.contents = contents;
31 this.category = category;
32 }
33
34 [PrimaryKey]
35 public int Id
36 {
37 get { return id; }
38 set { id = value; }
39 }
40
41 [Property]
42 public String Title
43 {
44 get { return title; }
45 set { title = value; }
46 }
47
48 [Property(ColumnType = "StringClob")]
49 public String Contents
50 {
51 get { return contents; }
52 set { contents = value; }
53 }
54
55 [Property]
56 public String Category
57 {
58 get { return category; }
59 set { category = value; }
60 }
61
62 [BelongsTo("blogid")]
63 public Blog Blog
64 {
65 get { return blog; }
66 set { blog = value; }
67 }
68
69 [Property("created")]
70 public DateTime Created
71 {
72 get { return created; }
73 set { created = value; }
74 }
75
76 [Property("published")]
77 public bool Published
78 {
79 get { return published; }
80 set { published = value; }
81 }
82
83 public static void DeleteAll()
84 {
85 ActiveRecordBase.DeleteAll(typeof(Post));
86 }
87
88 public static Post[] FindAll()
89 {
90 return (Post[])ActiveRecordBase.FindAll(typeof(Post));
91 }
92
93 public static Post[] GetPostsFromTitle(string title)
94 {
95 MyPostCustomQuery q = new MyPostCustomQuery();
96 q.MaxResults = 3;
97 q.Title = title;
98 return (Post[])ExecuteQuery(q);
99 }
100
101 public static Post[] GetPostsFromAuthor(String author)
102 {
103 return (Post[])Execute(typeof(Blog),
104 delegate(ISession session, object instance)
105 {
106 // create the query
107 IQuery query = session.CreateQuery("from Post p where p.Blog.Author = :author");
108
109 // set the parameters
110 query.SetString("author", (String)instance);
111
112 // fetch the results
113 IList results = query.List();
114
115 // OPTIONAL: convert the results to an array or
116 // something meaningful, instead of returning the IList
117 Post[] posts = new Post[results.Count];
118 results.CopyTo(posts, 0);
119
120 // return
121 return posts;
122 }, author);
123 }
124 }
125}
126