Ok, it quite a bit, but here it goes. I have left non-essential things out to keep the code a bit compact. I hope it will be enough. If you need something more, just ask me what.
The following code works for the InsuredEmployee, but not for the Employee. I hope you can figure out what I am doing wrong.
Mapping:
Code:
<class name=" HRM_BL.Person, HRM_BL" table="tbl_Person">
<id name="Id" type="Int32" column="Person_Id" access="field.pascalcase-underscore">
<generator class="identity" />
</id>
<discriminator column="P_Employee" />
<property name="FirstName" column="P_First_Name" type="String" />
<property name="LastName" column="P_Name" type="String" />
<property name="BirthDate" column="P_Date_Birth" type="DateTime" />
<property name="BirthPlace" column="P_Place_Birth" type="String" />
<property name="BirthCountry" column="P_Birth_Country_Id" type="Int32" />
<property name="Nationality" column="P_Nationality_Id" type="Int32" />
<property name="Language" column="P_Language_Cd" type="String" />
<property name="Gender" column="P_Gender" type="String"/>
<subclass name=" HRM_BL.Employee, HRM_BL" discriminator-value="E" >
<property name="GroupStartDate" column="P_Date_Group_Started" type="DateTime" />
<property name="PensionDate" column="P_Pension_Date" type="DateTime" />
<property name="PrePensionDate" column="P_PrePension_Date" type="DateTime" />
<subclass name=" HRM_BL.InsuredEmployee, HRM_BL" discriminator-value="I">
<property name="InsuranceStartDate" column="P_Insurance_Start_Date" type="DateTime" />
<property name="InsuranceEndDate" column="P_Insurance_End_Date" type="DateTime" />
</subclass>
</subclass>
</class>
Class declarations:
Code:
namespace HRM_BL
{
public class Person
{
private int _Id;
private string _firstName;
private string _lastName;
private DateTime _birthDate;
private string _birthPlace;
private int _birthCountry;
private int _nationality;
private string _language;
private string _gender;
public string Gender
{
get { return _gender; }
set { _gender = value; }
}
public string Language
{
get { return _language; }
set { _language = value; }
}
public int Nationality
{
get { return _nationality; }
set { _nationality = value; }
}
public int BirthCountry
{
get { return _birthCountry; }
set { _birthCountry = value; }
}
public string BirthPlace
{
get { return _birthPlace; }
set { _birthPlace = value; }
}
public DateTime BirthDate
{
get { return _birthDate; }
set { _birthDate = value; }
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
public int Id
{
get { return _Id; }
set { _Id = value; }
}
}
}
Code:
namespace HRM_BL
{
public class Employee : Person
{
private DateTime _groupStartDate;
private DateTime _pensionDate;
private DateTime _prePensionDate;
public DateTime PrePensionDate
{
get { return _prePensionDate; }
set { _prePensionDate = value; }
}
public DateTime PensionDate
{
get { return _pensionDate; }
set { _pensionDate = value; }
}
public DateTime GroupStartDate
{
get { return _groupStartDate; }
set { _groupStartDate = value; }
}
}
}
Code:
namespace HRM_BL
{
public class InsuredEmployee : Employee
{
private DateTime _insuranceStartDate;
private DateTime _insuranceEndDate;
public DateTime InsuranceStartDate
{
get { return _insuranceStartDate; }
set { _insuranceStartDate = value; }
}
public DateTime InsuranceEndDate
{
get { return _insuranceEndDate; }
set { _insuranceEndDate = value; }
}
}
}
The form where I load the data:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NHibernate.Cfg;
using NHibernate.Expression;
using NHibernate;
using System.Collections;
using Library_UI.Controls;
using Library_UI.TemplateForms;
using HRM_BL;
namespace HRM.Parameters
{
public class EmployeeOverview : GeneralOverviewForm
{
#region Declarations
private IDILabel lblDescription;
private IDISearchBox txtDescription;
private ColumnHeader columnHeader2;
IList _list;
ISession _session;
private ColumnHeader columnHeader3;
private IContainer components = null;
public ListViewItem objEmployeeItem;
#endregion
public EmployeeOverview()
{
InitializeComponent();
this.LoadData();
}
private void LoadData()
{
_session = NHibPersistor.SessionFactory.OpenSession();
_list = _session.CreateCriteria(typeof(InsuredEmployee)).List();
lvOverview.Items.Clear();
foreach (InsuredEmployee employee in _list)
{
objEmployeeItem = lvOverview.Items.Add(employee.Id.ToString());
objEmployeeItem.SubItems.Add(employee.LastName);
}
_session.Close();
}
}
}
the NHibpersistor:
Code:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
namespace HRM_BL
{
public static class NHibPersistor
{
private static Configuration _config;
private static ISessionFactory _factory;
public static ISessionFactory SessionFactory
{
get
{
if (_factory == null)
{
_config = new Configuration();
_config.AddAssembly("HRM_BL");
_factory = _config.BuildSessionFactory();
}
return _factory;
}
}
}
}