Rising

自我学习记录,方便使用时查找。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

如果有表Blog和Posts之间有一对多关系,那么如果二者关系对应外键属性代码如下所示:

        //一对多的一方添加以下字段和属性
        private IList<Post> posts=new List<Post>();
        [HasMany(Table="Posts", ColumnKey="blogid", Inverse=true, Cascade=ManyRelationCascadeEnum.AllDeleteOrphan)]
        public IList<Post> Posts
        {
            get { return posts=new List<Post>(); }
            set { posts=value; }
        }
则在使用以下代码时
            Blog blog = Blog.Find(1);//Blog表中有Id值为1的记录

会引发未处理的异常:

 HibernateException

详细信息为:

A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: ActiveRecordDemo.Blog.Posts
 

如果把Blog中的外键关系属性代码改为:

        //一对多的一方添加以下字段和属性
        private IList<Post> posts=new List<Post>();
        [HasMany(Table="Posts", ColumnKey="blogid", Inverse=true, Cascade=ManyRelationCascadeEnum.All)]
        public IList<Post> Posts
        {
            get { return posts=new List<Post>(); }
            set { posts=value; }
        }
 则
Blog blog = Blog.Find(1);

并不会引发异常。

 

此外,Blog实例中有属性:posts,此属性应对应Blog实例的子表中记录实体,但实际却为空(此时子表posts中有对应的子记录) ,不知如何得到子记录并存放在属性posts中。

posted on 2012-02-27 22:46  Rising  阅读(477)  评论(0编辑  收藏  举报