码迷,mamicode.com
首页 > 移动开发 > 详细

NHibernate 基于 ClassMapping 的 ManyToMany(多对多配置)

时间:2018-07-15 21:20:54      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:lib   set   virt   ssm   lse   rar   nat   tor   int   

NHibernate 基于 ClassMapping 的 ManyToMany(多对多配置)

实体类

public class Library
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<Book> Books { get; set; }

    public Library()
    {
        Books = new List<Book>();
    }
}

public class Book
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<Library> Libraries { get; set; }
    public virtual IList<BookLevel> BookLevel { get; set; }

    public Book()
    {
        Libraries = new List<Library>();
        BookLevel = new List<BookLevel>();
    }
}

public class BookLevel
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<Book> Book { get; set; }

    public BookLevel()
    {
        Book = new List<Book>();
    }
}

ClassMapping 映射类

public class LibraryMap : ClassMapping<Library>
{
    public LibraryMap()
    {
        Lazy(false);
        Table("Library");
        Id(l => l.Id,map => { map.Column("id"); map.Generator(Generators.Identity); });
        Property(x => x.Name, map => map.Column("LibraryName"));

        this.Bag(x => x.Books, map => {
            map.Table("Book_Library");
            map.Cascade(Cascade.All);
            map.Inverse(false);
            map.Lazy(CollectionLazy.Lazy);
            map.Key(key => key.Column("LibraryId"));
        }, m => m.ManyToMany(x => x.Column("BookId")));
    }
}

public class BookMap : ClassMapping<Book>
{
    public BookMap()
    {
        Lazy(false);
        Table("Book");
        Id(l => l.Id, map => { map.Column("id"); map.Generator(Generators.Identity); });
        Property(x => x.Name, map => map.Column("BookName"));

        this.Bag(x => x.Libraries, map => {
            map.Table("Book_Library");
            map.Cascade(Cascade.All);
            map.Inverse(false);
            map.Lazy(CollectionLazy.Lazy);
            map.Key(key => key.Column("BookId"));
        }, m => m.ManyToMany(x=>x.Column("LibraryId")));

        this.Bag(x => x.BookLevel, map => {
            map.Table("Book_Level");
            map.Cascade(Cascade.All);
            map.Inverse(false);
            map.Lazy(CollectionLazy.Lazy);
            map.Key(key => key.Column("BookId"));
        }, m => m.ManyToMany(x => x.Column("LevelId")));
    }
}

public class BookLevelMap : ClassMapping<BookLevel>
{
    public BookLevelMap()
    {
        Lazy(false);
        Table("BookLevel");
        Id(l => l.Id, map => { map.Column("id"); map.Generator(Generators.Identity); });
        Property(x => x.Name, map => map.Column("LevelName"));

        this.Bag(x => x.Book, map => {
            map.Table("Book_Level");
            map.Cascade(Cascade.All);
            map.Inverse(false);
            map.Lazy(CollectionLazy.Lazy);
            map.Key(key => key.Column("LevelId"));
        }, m => m.ManyToMany(x => x.Column("BookId")));
    }
}

NHibernate 基于 ClassMapping 的 ManyToMany(多对多配置)

标签:lib   set   virt   ssm   lse   rar   nat   tor   int   

原文地址:https://www.cnblogs.com/baily/p/9314431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!