One To Many

it2022-05-09  25

the one is Blog: 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 : ActiveRecordBase12    {13        private int id;14        private String name;15        private String author;16        private IList posts = new ArrayList();1718        public Blog()19        {20        }2122        public Blog(String name)23        {24            this.name = name;25        }2627        [PrimaryKey]28        public int Id29        {30            get return id; }31            set { id = value; }32        }3334        [Property]35        public String Name36        {37            get return name; }38            set { name = value; }39        }4041        [Property]42        public String Author43        {44            get return author; }45            set { author = value; }46        }4748        [HasMany(typeof(Post),49            Table = "Posts", ColumnKey = "blogid",50            Inverse = true, Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]51        public IList Posts52        {53            get return posts; }54            set { posts = value; }55        }5657        public static void DeleteAll()58        {59            ActiveRecordBase.DeleteAll(typeof(Blog));60        }6162        public static Blog[] FindAll()63        {64            return (Blog[])ActiveRecordBase.FindAll(typeof(Blog));65        }6667        public static Blog Find(int id)68        {69            return (Blog)ActiveRecordBase.FindByPrimaryKey(typeof(Blog), id);70        }7172        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        }7778    }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        }100101        public static Post[] GetPostsFromAuthor(String author)102        {103            return (Post[])Execute(typeof(Blog),104               delegate(ISession session, object instance)105               {106                   // create the query107                   IQuery query = session.CreateQuery("from Post p where p.Blog.Author = :author");108109                   // set the parameters110                   query.SetString("author", (String)instance);111112                   // fetch the results113                   IList results = query.List();114115                   // OPTIONAL: convert the results to an array or 116                   // something meaningful, instead of returning the IList117                   Post[] posts = new Post[results.Count];118                   results.CopyTo(posts, 0);119120                   // return121                   return posts;122               }, author);123        }124    }125}126

转载于:https://www.cnblogs.com/nanshouyong326/archive/2007/04/03/697857.html


最新回复(0)