0
votes

Composite Key mapping is not working in below scenario.

Database tables are as below.

   Employee { Emp_ID, Name, Role_ID } (Role_ID is foreign key from Role table);
   Leave    { Leave_ID, Leave_Date, Leave_Comment}; 
   Employee_Leave  { Emp_ID, Leave_ID, Approval }; (EMP_ID and Leave_ID are composite key from Employee and Leave table respectively)

Entity classes are as below. class Employee { public virtual string ID { get; set; } public virtual string Name { get; set; } public virtual Role EmpRole { get; set; } }

public class Leave
{
    virtual public Int16 LeaveID { get; set; }
    virtual public String LeaveDate { get; set; }
    virtual public String Comment { get; set; }
}

 public class EmployeeLeaveApproval
 {
    public virtual string EMP_ID { get; set; }
    public virtual int Leave_ID { get; set; }
    public virtual string Approval { get; set; }
 }

Mapping classes are as below.

 public class EmployeeMap : ClassMap<Employee>
 {
     public EmployeeMap()
     {
         Table("Employee");
         Id(x => x.ID, "ID");
         Map(x => x.Name, "NAME");
         References(x => x.EMPRole, "ROLE_ID").Not.LazyLoad();
      }
  }

   public class LeaveMap : ClassMap<Leave>
   {
       public LeaveMap()
       {
           Table("Leave");
           Id(x => x.LeaveID, "LEAVE_ID");
           Map(x => x.LeaveDate, "LEAVE_DATE");
           Map(x => x.Comment, "LEAVE_COMMENT");
       }
    }

Below class mapping is working fine.

    public class EmployeeLeaveApprovalMap : ClassMap<EmployeeLeaveApproval>
    {
       public EmployeeLeaveApprovalMap()
       {
           Table("Employee_Leave");
           Id(x => x.EMP_ID, "EMP_ID");
           Map(x => x.Leave_ID, "LEAVE_ID");
           Map(x => x.Approval, "Approval");
        }
     }

Below class mapping is not working.

    public class EmployeeLeaveApprovalMap : ClassMap<EmployeeLeaveApproval>
    {
       public EmployeeLeaveApprovalMap()
       {
           Table("Employee_Leave");
           CompositeId()
             .KeyProperty(x => x.EMP_ID, "EMP_ID")
             .KeyProperty(x => x.Leave_ID, "LEAVE_ID");
           Map(x => x.Approval, "Approval");
        }
     }

Getting error "Database was not configured through Database method." while calling method BuildSessionFactory.

Many many thanks in advance for any help.

1

1 Answers

1
votes

Found solution rather to say found the mistake i am doing. Equal and GetHashCode methods are left being implemented in my code. Below is the corrected entiity,

 public class EmployeeLeaveApproval : Object
{
    public virtual string EMP_ID { get; set; }
    public virtual int Leave_ID { get; set; }
    public virtual string Approval { get; set; }

    public EmployeeLeaveApproval() {}

    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;

        EmployeeLeaveApproval EL = (EmployeeLeaveApproval)obj;
        if (EL == null)
            return false;

        if (EMP_ID == EL.EMP_ID && Leave_ID == EL.Leave_ID)
            return true;

        return false;
    }

    public override int GetHashCode()
    {
        return (EMP_ID + "|" + Leave_ID).GetHashCode();
    }
}

regards..Dharmendra