zoukankan      html  css  js  c++  java
  • Castle ActiveRecord的一对多问题

    如果有表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中。

  • 相关阅读:
    Future和Callable的使用
    Tiny Jpeg Decoder (JPEG解码程序) 源代码分析 1:解码文件头
    jQuery 表格排序插件 Tablesorter 使用
    jQuery 表单验证插件 jQuery Validation Engine 使用
    jQuery 文本编辑器插件 HtmlBox 使用
    开源视频质量评价工具: IQA
    hql 语法与详细解释
    MYSQL常用命令
    C++发送HTTP请求获取网页HTML代码
    编译运行Red5源代码
  • 原文地址:https://www.cnblogs.com/Rising/p/2370557.html
Copyright © 2011-2022 走看看