0
public class Student
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public List<Literature> LiteratureCources { get; set; }
    public List<Drawing> DrawingCources { get; set; }
    public List<Internet> InternetCources { get; set; }
}

public class cource
{
    public int CourceId { get; set; }
    public string CourceName { get; set; }
}

public class Literature : cource
{
    public LiteratureType LiteratureType { get; set; }
}

public class Drawing : cource
{
    public DrawingType DrawingType { get; set; }
}

public class Internet : cource
{
    public InternetType InternetType { get; set; }
}
public enum LiteratureType
{
    L1, L2, L3
}

public enum DrawingType
{
    D1, D2, D3
}
public enum InternetType
{
    I1, I2, I3
}

Tables
------

Student
-------
StudentId
StudentName

Cource
------
CourceId (PK)
CourceName

InternetCource
--------------
InternetCourceId (FK)
InternetType

DrawingCource
-------------
DrawingCourceId (FK)
DrawingType

LiteratureCource
----------------
LiteratureCourceId (FK)
LiteratureType

StudentCource  (mybridge table)
-------------
StudentId 
CourceId

public class LiteratureCourceMap:ClassMap<Literature>
{
    public LiteratureCourceMap()
    {
        Table("Cource");
        ID(a=>a.CourceId,"CourceId");
        Map(a=>a.CourceName,"CourceName");

        Join("LiteratureCource",a=>{
            a.KeyColumn("CourceId");
        });
    }
}

same mapping for Drawing and Internet

public class StudentMap:ClassMap<Student>
{
    public StudentMap()
    {
        Table("Student");
        ID(a=>a.StudentId,"StudentId");
        Map(a=>a.StudentName,"StudentName");

        HasManyToMany(a=>a.InternetCources).Table("StudentCource").
            ParentKeyColumn("StudentId").
            ChildKeColumn("CourceId").
            Cascade.All();

         HasManyToMany(a=>a.DrawingCources).Table("StudentCource").
            ParentKeyColumn("StudentId").
            ChildKeColumn("CourceId").
            Cascade.All();

         HasManyToMany(a=>a.LiteratureCources).Table("StudentCource").
            ParentKeyColumn("StudentId").
            ChildKeColumn("CourceId").
            Cascade.All();
    }
}

now when if insert data it works fine data is correctly stored in corresponding tables however when if try to fetch data something goes wrong

lets say if insert 2 literature cources and 1 internet cource. when if fetch data from tables i have 3 literature records(2 records filled 1 empty or null) and 3 internet records(1 filled and 2 empty). pls help me correct mapping

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
Sumit
  • 196
  • 1
  • 8

1 Answers1

0

It seems, NHibernate always fetches from the Cource table, because there is the primary key. You should try Subclass mappings. See this example:

public class CourceMap : ClassMap<Cource>
{
  public CourceMap()
  {
    Id(x => x.CouerceId);
    Map(x => x.CourceName);
  }
}

public class DrawingMap : SubclassMap<Cource>
{
  public DrawingMap()
  {
    Map(x => x.DrawingType);
  }
}

Refer to this for further reading: https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping.

rumpelstiefel
  • 466
  • 3
  • 5