zoukankan      html  css  js  c++  java
  • 使用NHibernate(7)-- 一对一 && 一对多 && 多对多

    1, 一对一。

    对于数据量比较大的时候,考虑查询的性能,肯能会把一个对象的属性分到两个表中存放;比如用户和用户资料,经常使用的一般是Id和用户名,用户资料(学校,籍贯等)是不经常被查询的,所以就会分成两个表。实体和映射如下:

    使用以下代码插入数据:

    public ActionResult InitData()

            {

                using (var session = MvcApplication.SessionFactory.OpenSession(new DebugInterceptor()))

                using (var transaction = session.BeginTransaction())

                {

                    try

                    {

                        for (int i = 0; i < 10; i++)

                        {

                            User user = new Models.User

                            {

                                Name = "FuzhePan_" + i

                            };

                            session.Save(user);

                            UserProfile profile = new UserProfile

                                {

                                    UserId = user.Id,

                                    Address = "山东山东山东_" + i,

                                    School = "chsoolssss_" + i

                                };

                            session.Save(profile);

                        }

                        transaction.Commit();

                    }

                    catch (Exception)

                    {

                        transaction.Rollback();

                        throw;

                    }

                }

                return Content("搞定");

            }

    以下代码进行数据展示:

    clipboard[1]

    结果:

    clipboard[2]

    2,一对多 && 多对一。

    一对多的关系是最常用的,比如在一个问答系统中,一个用户对应多个问题。

    首先添加用户、问题实体和相应映射:

    [Class(Table = "NH_User")]

        public class User

        {

            [Id(1, Name = "Id", TypeType = typeof(long), Column = "Id", UnsavedValue = "0")]

            [Generator(2, Class = "native")]

            public virtual long Id { get; set; }

            [PropertyAttribute(Column = "Name", TypeType = typeof(string))]

            public virtual string Name { get; set; }

            [OneToOne]

            public virtual UserProfile UserProfile { get; set; }

            [Bag(0, Table = "Question", Lazy = CollectionLazy.True, Cascade ="all")]

            [Key(1, Column = "UserId")]

            [OneToManyAttribute(2, ClassType = typeof(Question))]

            public virtual List<Question> Questions { get; set; }

        }

        [Class]

        public class Question

        {

            [Id(1, Name = "Id")]

            [Generator(2, Class = "native")]

            public virtual long Id { get; set; }

            [Property]

            public virtual long UserId { get; set; }

            [Property]

            public virtual string Subject { get; set; }

            [Property]

            public virtual string Body { get; set; }

             [ManyToOne(0, Column = "UserId", Insert = false, Update = false)]

            public User User { get; set; }

        }

    User实体添加了一个Question集合,并配置其对应着Question表,Lazy设置延迟加载,通过Key.Column配置逻辑外键是Question中的UserId,通过OneToMany配置用户和问题是一对多的关系。

    在Question中,通过ManyToOne配置多队以的关系,Insert=false和Update=false表示当我们插入或修改一个Question的时候并不会级联插入或修改其所对应的User实体。

    Bag其实是NHibernate所依赖的Iesi.Collections.dll库中所提供的集合之一,下面对所有集合进行说明:

    Bag:对象集合,每个元素可以重复。在.Net中相当于IList或者IList<T>实现。

    Set:对象集合,每个元素必须唯一。在.Net中相当于ISet或者ISet<T>实现。

    List:整数索引对象集合,每个元素可以重复,在.Net中相当于ArraryList或者List<T>实现。

    Map:键值对集合。在.Net中相当于HashTable或者IDictionary<Tkey,TValue>实现。

    父级和子级的关系通过inverse配置,级联操作通过Cascade控制。对于一对多的情况,类之间的关系默认是由父级来维护,如果想让自己维护,则需要inverse=true。

    Casecade控制级联操作,值有:,nonesave-updatedeleteallall-delete-ophan,默认值为none。

    none:不进行级联操作,但删除父级时,会将子级的外键置为null。

    save-update:保存和修改父级的子级集合时,会同步到数据库中。

    delete:删除操作同步到数据库。

    all:包括所有,但删除父级时,会将父级关联的外键置为null。

    all-delete-ophan:和all的区别是,删除父级时,会级联删除关联的子级。

    3,多对多。

    多对多的情况,一般通过一个中间表转换成一对多的形式。但NHibernate支持直接配置两个类是多对多,从而忽略中间表的存在。这种情况不常用,故略之。

  • 相关阅读:
    常见消息引擎系统对比
    kafka(一)入门
    pycharm工具的使用
    VMware下安装Ubantu 18.04
    VMware虚拟机下Ubuntu安装VMware Tools详解
    python--or 和 and 表达式
    django使用缓存之drf-extensions
    数据结构--线性表之链表
    Redis配置主从时报错“Could not connect to Redis at 192.168.0.50:6379: Connection refused not connected>”
    Rsync+sersync(inotify)实现数据实时双向同步
  • 原文地址:https://www.cnblogs.com/FuzhePan/p/3652142.html
Copyright © 2011-2022 走看看