0

In brief: query to "parents" table results "parents" + records from "children". "children" have FKs to "parents" and therefore I have as so many duplictes as quantity of FKs in "children". I'm using Fluent.NHibernate in ASP.NET MVC3 project.

So, DB looks like this

TABLE parents
***id
***system_name

TABLE children
***id
***custom_name
***parent_id

FK: parent_id === "FK_parent_id_to_id_in_parents"

Model:

public class Parent
{
   public virtual int Id { get; set; }
   public virtual string SystemName { get; set; }
}

public class Child: Parent
{
   public override int Id
   {
      get
      {
             return base.Id;
       }
      set
      {
             base.Id = value;
      }
   }
   public virtual string CustomName
   {
      get
      {
             return _CustomName != null ? _CustomName : base.SystemName;
      }
     set
     {
            _CustomName = value;
     }
   }

    public virtual Parent ParentId { get; set; }
    private string _CustomName = string.Empty;
}

Mappings:

public class ParentMapper: ClassMap<Parent>
{
    Table("parents");
    Id(x => x.Id).GeneratedBy.Native();
    Map(x => x.SystemName, "system_name").Not.Nullable();
}

public class ChildMapper: ClassMap<Child>
{
    Table("children");
    Id(x => x.Id).GeneratedBy.Native();
    Map(x => x.DisplayName, "display_name");
    References(x => x.ParentId).Column("parent_id").Cascade.None().ForeignKey("FK_parent_id_to_id_in_parents");
}

Query:

using(ITransaction tr = session.BeginTransaction())
{
    List<Parent> lp = new List<Parent>(session.CreateCriteria(typeof(Parent)).List<Parent>());
    tr.Commit();
}
Cheekysoft
  • 35,194
  • 20
  • 73
  • 86
Victor Ponamarev
  • 179
  • 2
  • 11

1 Answers1

0

This seems incorrect to me and I am not sure why you are overidding Get Id in your child class. Wht are you returning parent.id for child.id?

 public override int Id
   {
      get
      {
             return base.Id;
       }
      set
      {
             base.Id = value;
      }
   }

Surely it should be?

public int id { get; set;}

If you do want this then you will need to also override the following in your child class so NHibernate knows what a unique child record is.

public override int GetHashCode() 
override bool Equals(object obj)

See here or here for more info

Community
  • 1
  • 1
Rippo
  • 22,117
  • 14
  • 78
  • 117