One To Many
the one is Blog:

 Blog
Blog
1 using System;
using System;
2 using System.Collections;
using System.Collections;
3
4 using Castle.ActiveRecord;
using Castle.ActiveRecord;
5 using Castle.ActiveRecord.Queries;
using Castle.ActiveRecord.Queries;
6 using NHibernate.Expression;
using NHibernate.Expression;
7
8 namespace CastleLearning.OneToMany
namespace CastleLearning.OneToMany
9

 {
{
10 [ActiveRecord]
    [ActiveRecord]
11 public class Blog : ActiveRecordBase
    public class Blog : ActiveRecordBase
12
 
     {
{
13 private int id;
        private int id;
14 private String name;
        private String name;
15 private String author;
        private String author;
16 private IList posts = new ArrayList();
        private IList posts = new ArrayList();
17
18 public Blog()
        public Blog()
19
 
         {
{
20 }
        }
21
22 public Blog(String name)
        public Blog(String name)
23
 
         {
{
24 this.name = name;
            this.name = name;
25 }
        }
26
27 [PrimaryKey]
        [PrimaryKey]
28 public int Id
        public int Id
29
 
         {
{
30
 get
            get  { return id; }
{ return id; }
31
 set
            set  { id = value; }
{ id = value; }
32 }
        }
33
34 [Property]
        [Property]
35 public String Name
        public String Name
36
 
         {
{
37
 get
            get  { return name; }
{ return name; }
38
 set
            set  { name = value; }
{ name = value; }
39 }
        }
40
41 [Property]
        [Property]
42 public String Author
        public String Author
43
 
         {
{
44
 get
            get  { return author; }
{ return author; }
45
 set
            set  { author = value; }
{ author = value; }
46 }
        }
47
48 [HasMany(typeof(Post),
        [HasMany(typeof(Post),
49 Table = "Posts", ColumnKey = "blogid",
            Table = "Posts", ColumnKey = "blogid",
50 Inverse = true, Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
            Inverse = true, Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
51 public IList Posts
        public IList Posts
52
 
         {
{
53
 get
            get  { return posts; }
{ return posts; }
54
 set
            set  { posts = value; }
{ posts = value; }
55 }
        }
56
57 public static void DeleteAll()
        public static void DeleteAll()
58
 
         {
{
59 ActiveRecordBase.DeleteAll(typeof(Blog));
            ActiveRecordBase.DeleteAll(typeof(Blog));
60 }
        }
61
62 public static Blog[] FindAll()
        public static Blog[] FindAll()
63
 
         {
{
64 return (Blog[])ActiveRecordBase.FindAll(typeof(Blog));
            return (Blog[])ActiveRecordBase.FindAll(typeof(Blog));
65 }
        }
66
67 public static Blog Find(int id)
        public static Blog Find(int id)
68
 
         {
{
69 return (Blog)ActiveRecordBase.FindByPrimaryKey(typeof(Blog), id);
            return (Blog)ActiveRecordBase.FindByPrimaryKey(typeof(Blog), id);
70 }
        }
71
72 public static Blog[] FindByBlogName(string blogName)
        public static Blog[] FindByBlogName(string blogName)
73
 
         {
{
74 SimpleQuery<Blog> q = new SimpleQuery<Blog>(typeof(Blog),@"from Blog b where b.Name = ?",blogName);
            SimpleQuery<Blog> q = new SimpleQuery<Blog>(typeof(Blog),@"from Blog b where b.Name = ?",blogName);
75 return q.Execute();
            return q.Execute();
76 }
        }
77
78 }
    }
79 }
}
80 
the many is:

 Post
Post
1 using System;
using System;
2 using System.Collections;
using System.Collections;
3 using Castle.ActiveRecord;
using Castle.ActiveRecord;
4 using NHibernate;
using NHibernate;
5
6 namespace CastleLearning.OneToMany
namespace CastleLearning.OneToMany
7

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

 Blog
Blog1
 using System;
using System;2
 using System.Collections;
using System.Collections;3

4
 using Castle.ActiveRecord;
using Castle.ActiveRecord;5
 using Castle.ActiveRecord.Queries;
using Castle.ActiveRecord.Queries;6
 using NHibernate.Expression;
using NHibernate.Expression;7

8
 namespace CastleLearning.OneToMany
namespace CastleLearning.OneToMany9


 {
{10
 [ActiveRecord]
    [ActiveRecord]11
 public class Blog : ActiveRecordBase
    public class Blog : ActiveRecordBase12

 
     {
{13
 private int id;
        private int id;14
 private String name;
        private String name;15
 private String author;
        private String author;16
 private IList posts = new ArrayList();
        private IList posts = new ArrayList();17

18
 public Blog()
        public Blog()19

 
         {
{20
 }
        }21

22
 public Blog(String name)
        public Blog(String name)23

 
         {
{24
 this.name = name;
            this.name = name;25
 }
        }26

27
 [PrimaryKey]
        [PrimaryKey]28
 public int Id
        public int Id29

 
         {
{30

 get
            get  { return id; }
{ return id; }31

 set
            set  { id = value; }
{ id = value; }32
 }
        }33

34
 [Property]
        [Property]35
 public String Name
        public String Name36

 
         {
{37

 get
            get  { return name; }
{ return name; }38

 set
            set  { name = value; }
{ name = value; }39
 }
        }40

41
 [Property]
        [Property]42
 public String Author
        public String Author43

 
         {
{44

 get
            get  { return author; }
{ return author; }45

 set
            set  { author = value; }
{ author = value; }46
 }
        }47

48
 [HasMany(typeof(Post),
        [HasMany(typeof(Post),49
 Table = "Posts", ColumnKey = "blogid",
            Table = "Posts", ColumnKey = "blogid",50
 Inverse = true, Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
            Inverse = true, Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]51
 public IList Posts
        public IList Posts52

 
         {
{53

 get
            get  { return posts; }
{ return posts; }54

 set
            set  { posts = value; }
{ posts = value; }55
 }
        }56

57
 public static void DeleteAll()
        public static void DeleteAll()58

 
         {
{59
 ActiveRecordBase.DeleteAll(typeof(Blog));
            ActiveRecordBase.DeleteAll(typeof(Blog));60
 }
        }61

62
 public static Blog[] FindAll()
        public static Blog[] FindAll()63

 
         {
{64
 return (Blog[])ActiveRecordBase.FindAll(typeof(Blog));
            return (Blog[])ActiveRecordBase.FindAll(typeof(Blog));65
 }
        }66

67
 public static Blog Find(int id)
        public static Blog Find(int id)68

 
         {
{69
 return (Blog)ActiveRecordBase.FindByPrimaryKey(typeof(Blog), id);
            return (Blog)ActiveRecordBase.FindByPrimaryKey(typeof(Blog), id);70
 }
        }71

72
 public static Blog[] FindByBlogName(string blogName)
        public static Blog[] FindByBlogName(string blogName)73

 
         {
{74
 SimpleQuery<Blog> q = new SimpleQuery<Blog>(typeof(Blog),@"from Blog b where b.Name = ?",blogName);
            SimpleQuery<Blog> q = new SimpleQuery<Blog>(typeof(Blog),@"from Blog b where b.Name = ?",blogName);75
 return q.Execute();
            return q.Execute();76
 }
        }77

78
 }
    }79
 }
}80

the many is:

 Post
Post1
 using System;
using System;2
 using System.Collections;
using System.Collections;3
 using Castle.ActiveRecord;
using Castle.ActiveRecord;4
 using NHibernate;
using NHibernate;5

6
 namespace CastleLearning.OneToMany
namespace CastleLearning.OneToMany7


 {
{8

9
 [ActiveRecord]
    [ActiveRecord]10
 public class Post : ActiveRecordBase
    public class Post : ActiveRecordBase11

 
     {
{12
 private int id;
        private int id;13
 private String title;
        private String title;14
 private String contents;
        private String contents;15
 private String category;
        private String category;16
 private DateTime created;
        private DateTime created;17
 private bool published;
        private bool published;18
 private Blog blog;
        private Blog blog;19

20
 public Post()
        public Post()21

 
         {
{22
 created = DateTime.Now;
            created = DateTime.Now;23
 }
        }24

25
 public Post(Blog blog, String title, String contents, String category)
        public Post(Blog blog, String title, String contents, String category)26
 : this()
            : this()27

 
         {
{28
 this.blog = blog;
            this.blog = blog;29
 this.title = title;
            this.title = title;30
 this.contents = contents;
            this.contents = contents;31
 this.category = category;
            this.category = category;32
 }
        }33

34
 [PrimaryKey]
        [PrimaryKey]35
 public int Id
        public int Id36

 
         {
{37

 get
            get  { return id; }
{ return id; }38

 set
            set  { id = value; }
{ id = value; }39
 }
        }40

41
 [Property]
        [Property]42
 public String Title
        public String Title43

 
         {
{44

 get
            get  { return title; }
{ return title; }45

 set
            set  { title = value; }
{ title = value; }46
 }
        }47

48
 [Property(ColumnType = "StringClob")]
        [Property(ColumnType = "StringClob")]49
 public String Contents
        public String Contents50

 
         {
{51

 get
            get  { return contents; }
{ return contents; }52

 set
            set  { contents = value; }
{ contents = value; }53
 }
        }54

55
 [Property]
        [Property]56
 public String Category
        public String Category57

 
         {
{58

 get
            get  { return category; }
{ return category; }59

 set
            set  { category = value; }
{ category = value; }60
 }
        }61

62
 [BelongsTo("blogid")]
        [BelongsTo("blogid")]63
 public Blog Blog
        public Blog Blog64

 
         {
{65

 get
            get  { return blog; }
{ return blog; }66

 set
            set  { blog = value; }
{ blog = value; }67
 }
        }68

69
 [Property("created")]
        [Property("created")]70
 public DateTime Created
        public DateTime Created71

 
         {
{72

 get
            get  { return created; }
{ return created; }73

 set
            set  { created = value; }
{ created = value; }74
 }
        }75

76
 [Property("published")]
        [Property("published")]77
 public bool Published
        public bool Published78

 
         {
{79

 get
            get  { return published; }
{ return published; }80

 set
            set  { published = value; }
{ published = value; }81
 }
        }82

83
 public static void DeleteAll()
        public static void DeleteAll()84

 
         {
{85
 ActiveRecordBase.DeleteAll(typeof(Post));
            ActiveRecordBase.DeleteAll(typeof(Post));86
 }
        }87

88
 public static Post[] FindAll()
        public static Post[] FindAll()89

 
         {
{90
 return (Post[])ActiveRecordBase.FindAll(typeof(Post));
            return (Post[])ActiveRecordBase.FindAll(typeof(Post));91
 }
        }92

93
 public static Post[] GetPostsFromTitle(string title)
        public static Post[] GetPostsFromTitle(string title)94

 
         {
{95
 MyPostCustomQuery q = new MyPostCustomQuery();
            MyPostCustomQuery q = new MyPostCustomQuery();96
 q.MaxResults = 3;
            q.MaxResults = 3;97
 q.Title = title;
            q.Title = title;98
 return (Post[])ExecuteQuery(q);
            return (Post[])ExecuteQuery(q);99
 }
        }100

101
 public static Post[] GetPostsFromAuthor(String author)
        public static Post[] GetPostsFromAuthor(String author)102

 
         {
{103
 return (Post[])Execute(typeof(Blog),
            return (Post[])Execute(typeof(Blog),104
 delegate(ISession session, object instance)
               delegate(ISession session, object instance)105

 
                {
{106
 // create the query
                   // create the query
107
 IQuery query = session.CreateQuery("from Post p where p.Blog.Author = :author");
                   IQuery query = session.CreateQuery("from Post p where p.Blog.Author = :author");108

109
 // set the parameters
                   // set the parameters
110
 query.SetString("author", (String)instance);
                   query.SetString("author", (String)instance);111

112
 // fetch the results
                   // fetch the results
113
 IList results = query.List();
                   IList results = query.List();114

115
 // OPTIONAL: convert the results to an array or
                   // OPTIONAL: convert the results to an array or 116
 // something meaningful, instead of returning the IList
                   // something meaningful, instead of returning the IList117
 Post[] posts = new Post[results.Count];
                   Post[] posts = new Post[results.Count];118
 results.CopyTo(posts, 0);
                   results.CopyTo(posts, 0);119

120
 // return
                   // return121
 return posts;
                   return posts;122
 }, author);
               }, author);123
 }
        }124
 }
    }125
 }
}126

.jpg) 
  
 
                    
                
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号