nHibernate Mapping By Code - Many to Many

Mapping by code是nHibernate3.2新增的功能,网络上及官方doc相关的介绍都很少。下面是如何使用mapping by code的方式配置多对多关联的例子。

 

关于如何配置nhibernate使用mapping by code,参考nHibernate Mapping By Code - Introduction

 

1. 实体类

public class Student
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Course> Courses { get; set; }
}

public class Course
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; } 
    public virtual IList<Student> Students { get; set; } 
}

 

2. 数据库表(基于MS SQL Server 2012)

CREATE TABLE [dbo].[Student](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NOT NULL,    
 CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED ([Id] ASC)

CREATE TABLE [dbo].[Course](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NOT NULL,    
 CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED ([Id] ASC)

CREATE TABLE [dbo].[Student_Course](
    [StudentId] [int] NOT NULL,
    [CourseId] [int] NOT NULL,    
 CONSTRAINT [PK_Student_Course] PRIMARY KEY CLUSTERED ([StudentId] ASC, [CourseId] ASC)

ALTER TABLE [dbo].[Student_Course] ADD CONSTRAINT [FK_Student_Course_StudentId] FOREIGN KEY([StudentId])
REFERENCES [dbo].[Student] ([Id])

ALTER TABLE [dbo].[Student_Course] ADD CONSTRAINT [FK_Student_Course_CourseId] FOREIGN KEY([CourseId])
REFERENCES [dbo].[Course] ([Id])

 

3.  映射类

public class CourseMapping : ClassMapping<Course>
{
    public CourseMapping()
    {
        Table("Course");
        Id(course => course.Id, map => map.Generator(Generators.Identity));
        Property(course => course.Name);
        Bag(course => course.Students, map =>
        {
            map.Table("Student_Course");
            map.Key(keyMapper => keyMapper.Column("CourseId"));
        }, rel => rel.ManyToMany(m => m.Column("StudentId")));
    }
}

public class StudentMapping : ClassMapping<Student>
{
    public StudentMapping()
    {
        Table("Student");
        Id(student => student.Id, map => map.Generator(Generators.Identity));
        Property(student => student.Name);
        Bag(student => student.Courses, map =>
            {
                map.Table("Student_Course");
                map.Key(keyMapper => keyMapper.Column("StudentId"));
            }, rel => rel.ManyToMany(m => m.Column("CourseId")));
    }
}

 


示例代码基于nHibernate3.3.1

转载请注明出处

posted @ 2012-07-01 22:36  iamzhenyu  阅读(340)  评论(0编辑  收藏  举报