I tried the below statements but it still does not do anything. I am including my Person class, Mapping file for the class and code to retreive data.
namespace SHIFT.People
{
/// <summary>
/// TABLE [dbo].[person_prsn]
/// </summary>
public class Person
{
/// <summary>
/// <pk>[prsn_pin] [int] NOT NULL ,
/// </summary>
private int mPin;
/// <summary>
/// [titl_title_code] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
/// </summary>
private string mTitleCode;
/// <summary>
/// [prsn_first_name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
/// </summary>
private string mFirstName;
/// <summary>
/// [prsn_last_name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
/// </summary>
private string mLastName;
/// <summary>
/// [prsn_former_last_name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS
/// NULL ,
/// </summary>
private string mFormerLastName;
/// <summary>
/// [prsn_primary_addr_num] [tinyint] NOT NULL ,
/// </summary>
private int mPrimaryAddressNumber;
/// <summary>
/// [prsn_gender] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
/// </summary>
private char mGender;
/// <summary>
/// [prsn_birth_dt] [datetime] NULL ,
/// A person must be between age 12 and 100 (removed CR081).
/// </summary>
private DateTime mDateOfBirth;
/// <summary>
/// [prsn_soc_ins_number] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
/// </summary>
private string mSocialInsuranceNumber;
/// <summary>
/// [ctry_citizenship_country_num] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS
/// NULL ,
/// </summary>
private char [] mCitizenshipCountryNumber;
/// <summary>
/// [imms_immigration_code] [varchar] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
/// </summary>
private string mImmigrationStatusCode;
/// <summary>
/// [prsn_web_url] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
/// </summary>
private string mWebUrl;
/// <summary>
/// [poaw_post_award_num] [smallint] NULL ,
/// </summary>
private int mPostAwardStatusNumber;
/// <summary>
/// [prsn_post_award_position] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS
/// NULL ,
/// </summary>
private string mPostAwardPosition;
/// <summary>
/// [inst_post_award_instit_num] [char] (6) COLLATE SQL_Latin1_General_CP1_CI_AS
/// NULL ,
/// </summary>
private char [] mPostAwardInstitutionNumber;
/// <summary>
/// [prsn_post_award_upd_dt] [datetime] NULL ,
/// </summary>
private DateTime mPostAwardUpdatedDate;
/// <summary>
/// [prsn_last_upd_datime] [datetime] NOT NULL ,
/// </summary>
private DateTime mLastUpdateDateTime;
/// <summary>
/// [prsn_last_upd_user] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
/// NULL ,
/// </summary>
private string mLastUpdateUser;
/// <summary>
/// [prsn_work_desc] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL
/// </summary>
private string mWorkDescription;
/// <summary>
/// Title where title.Code = this.TitleCode;
/// </summary>
private Title title;
/// <summary>
/// Address where address.Number is self.PrimaryAddressNumber
/// </summary>
private Address primaryAddress;
/// <summary>
/// Addresses where Address.PersonPin = this.Pin
/// </summary>
private Address [] addresses;
/// <summary>
/// PersonMesh where personMesh.PersonPon = this.Pin
/// </summary>
private PersonMedicalSubjectHeading [] peopleMeshes;
/// <summary>
/// PersonSpecialCondition where personSpecialCondition.PersonPin = this.Pin
/// </summary>
private PersonSpecialCondition [] personSpecialConditions;
/// <summary>
/// PersonDegree wher personDegree.PersonPin = this.Pin
/// </summary>
private PersonDegree [] personDegrees;
/// <summary>
/// PersonPublication where personPublication.PersonPin = this.Pin
/// </summary>
private PersonPublication [] personPublications;
/// <summary>
/// Country where Number = this.CitizenshipCountryNumber
/// </summary>
private Country citizenship;
/// <summary>
/// ImmigrationStatus where immigrationStatus.Code = this.ImmigrationStatusCode
/// </summary>
private ImmigrationStatus immigrationStatus;
/// <summary>
/// PostAwardStatus where postAwardStatus.Number = this.PostAwardStatusNumber;
/// </summary>
private PostAwardStatus postAwardStatus;
/// <summary>
/// Institution where institution.Number = this.PostAwardInstitutionNumber
/// </summary>
private Institution postAwardInstitution;
private PersonMedicalSubjectHeading personMedicalSubjectHeadings;
public Person(){}
~Person(){}
public virtual void Dispose(){}
/// <param name="pin"></param>
/// <param name="lastUpdateDate"></param>
/// <param name="LastUpdateUser"></param>
protected Person(int pin,
DateTime lastUpdateDate,
string LastUpdateUser) { }
/// <summary>
/// [read] generated by sequence generator
/// </summary>
public int Pin
{
get { return this.mPin; }
set { this.mPin = value; }
}
/// <summary>
/// [read][write]
/// </summary>
public string Title
{
get { return this.mTitleCode; }
set { this.mTitleCode = value; }
}
/// <summary>
/// [read][write]
/// </summary>
public string FirstName
{
get { return this.mFirstName; }
set { this.mFirstName = value; }
}
/// <summary>
/// [read][write]
/// </summary>
public string LastName
{
get { return this.mLastName; }
set { this.mLastName = value; }
}
/// <summary>
/// [read][write]
/// </summary>
public string FormerLastName
{
get { return this.mFormerLastName; }
set { this.mFormerLastName = value; }
}
/// <summary>
/// [read][write] returns the primary Address.
/// </summary>
public Address PrimaryAddress
{
get { return this.primaryAddress; }
}
/// <summary>
/// [read][write]
/// </summary>
public char Gender
{
get { return this.mGender; }
set { this.mGender = value; }
}
/// <summary>
/// [read][write]
/// </summary>
public DateTime DateOfBirth
{
get { return this.mDateOfBirth; }
set { this.mDateOfBirth = value; }
}
/// <summary>
/// [read][write]
/// </summary>
public string SocialInsuranceNumber
{
get { return this.mSocialInsuranceNumber; }
set { this.mSocialInsuranceNumber = value; }
}
/// <summary>
/// [read][write]
/// </summary>
public string WebUrl
{
get { return this.mWebUrl; }
set { this.mWebUrl = value; }
}
/// <summary>
/// [read][write]
/// </summary>
public PostAwardStatus PostAwardStatus
{
get { return this.postAwardStatus; }
}
/// <summary>
/// [read][write]
/// </summary>
public string PostAwardPosition
{
get { return this.mPostAwardPosition; }
set { this.mPostAwardPosition = value; }
}
/// <summary>
/// [read][write]
/// </summary>
public DateTime PostAwardUpdatedDate
{
get { return this.mPostAwardUpdatedDate; }
}
/// <summary>
/// [read][write]
/// </summary>
public DateTime LastUpdateDateTime
{
get { return this.mLastUpdateDateTime; }
}
/// <summary>
/// [read][write]
/// </summary>
public string WorkDescription
{
get { return this.mWorkDescription; }
set { this.mWorkDescription = value; }
}
/// <summary>
/// [read] returns a list of Addresses, including the primary one.
/// </summary>
public IList Addresses
{
get { return this.addresses; }
}
/// <summary>
/// [read][write] returns ImmigrationStatus
/// </summary>
public ImmigrationStatus ImmigrationStatus
{
get { return this.immigrationStatus; }
set { this.immigrationStatus = value; }
}
/// <summary>
/// [read][write]
/// </summary>
public String LastUpdatedUser
{
get { return this.mLastUpdateUser; }
set { this.mLastUpdateUser = value; }
}
/// <summary>
/// [read][write]
/// </summary>
public Country CitizenshipCountry
{
get { return this.citizenship; }
set { this.citizenship = value; }
}
/// <summary>
/// [read][write]
/// </summary>
public Institution PostAwardInstitution
{
get { return this.postAwardInstitution; }
set { this.postAwardInstitution = value; }
}
/// <summary>
/// returns lastName, firstName as string
/// </summary>
public override string ToString()
{
return this.mLastName + this.mFirstName;
}
/// <summary>
/// returns false if referenced by: CommitteeMember, ExternalReview,
/// ApplicationRole, or Trainee
/// </summary>
private bool IsDeletable(CommitteeMember committeeMemeber,
ExternalReview externalReview,
ApplicationRole applictaionRole,
Trainee trainee)
{
return false;
}
/// <summary>
/// must also delete: PersonDegrees, PersonSpecialCondition,
/// PersonMedicalSubjectHeadings, PersonPublications, and Addresses.
/// </summary>
protected void Delete(){}
/// <summary>
/// returns the next available address number. This is the (highest address number
/// for all Addresses) + 1. If there are no addresses, return 1. This will also
/// make this the primary address.
/// </summary>
//public int GetNextAddressNumber(){}
}//end Person
}//end namespace People
My mapping file for this class is SHIFT.hbm.xml is below and I have set it as embedded resource in the properties.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="SHIFT.People.Country, SHIFT" table="dbo.country_ctry">
<id name="mNumber" column="ctry_country_num">
<generator class="identity" />
</id>
<property name="mDescription" column="ctry_country_desc" />
<set name="mCitizenshipCountryNumber">
<key column="ctry_country_num" />
<one-to-many class="SHIFT.People.Person"/>
</set>
</class>
<class name="SHIFT.People.ImmigrationStatus, SHIFT" table="dbo.immigration_status_imms">
<id name="mCode" column="imms_immigration_code">
<generator class="assigned" />
</id>
<property name="mDescription" column="imms_immigration_desc" />
<set name="mImmigrationStatusCode">
<key column="imms_immigration_code" />
<one-to-many class="SHIFT.People.Person"/>
</set>
</class>
<class name="SHIFT.People.Person, SHIFT" table ="dbo.person_prsn">
<id name="mPin" column ="prsn_pin">
<generator class="identity" />
</id>
<many-to-one name="mTitleCode" class="SHIFT.People.Title, SHIFT" column ="titl_title_code"/>
<property name="mTitleCode" column="titl_title_code" type="string" />
<property name="mFirstName" column="prsn_first_name" type="string" />
<property name="mLastName" column="prsn_last_name" type="string" />
<property name="mFormerLastName" column="prsn_former_last_name" type="string" />
<property name="mPrimaryAddressNumber" column="prsn_primary_addr_num" type="int" />
<property name="mGender" column="prsn_gender" type="string" />
<property name="mDateOfBirth" column="prsn_birth_dt" type="datetime" />
<property name="mSocialInsuranceNumber" column="prsn_soc_ins_number" type="string" />
<many-to-one name="mCitizenshipCountryNumber" class="SHIFT.People.Country" column="ctry_citizenship_country_num" />
<many-to-one name="mImmigrationStatusCode" class="SHIFT.People.ImmigrationStatus" column="imms_immigration_code" />
<property name="mWebUrl" column="prsn_web_url" type="string" />
<many-to-one name="mPostAwardStatusNumber" class="SHIFT.People.PostAwardStatus" column="poaw_post_award_num" />
<property name="mPostAwardPosition" column="prsn_post_award_position" type="string" />
<property name="mPostAwardUpdatedDate" column="prsn_post_award_upd_dt" type="datetime" />
<property name="mLastUpdateDateTime" column="prsn_last_upd_datime" type="datetime" />
<property name="mLastUpdateUser" column="prsn_last_upd_user" type="string" />
<property name="mWorkDescription" column="prsn_work_desc" type="string" />
</class>
<class name="SHIFT.People.PostAwardStatus, SHIFT" table="dbo.post_award_status_poaw">
<id name="mNumber" column="poaw_post_award_num">
<generator class="assigned" />
</id>
<property name="mDescription" column="poaw_post_award_desc" type="string" />
<set name="mPostAwardStatusNumber">
<key column="poaw_post_award_num" />
<one-to-many class="SHIFT.People.Person"/>
</set>
</class>
<class name="SHIFT.People.Title, SHIFT" table="dbo.title_titl">
<id name="mCode" column="titl_title_code">
<generator class="assigned" />
</id>
<property name="mDescription" column="titl_title_desc" />
<set name="mTitleCode">
<key column="titl_title_code" />
<one-to-many class="SHIFT.People.Person"/>
</set>
</class>
</hibernate-mapping>
My code in which I am retreiving data looks like this:
private void SearchButton_Click(object sender, EventArgs e)
{
int PersonPin = Int32.Parse(PinTextBox.Text);
LookUpPerson(PersonPin);
}
private void LookUpPerson(int PersonPin)
{
Person person = new Person();
ISession session = null;
try
{
session = Global.NHSession;
DetachedCriteria criteria = DetachedCriteria.For<Person>();
criteria.Add(Expression.Eq("prsn_pin", PersonPin));
criteria.GetExecutableCriteria(session).List<Person>().Add(person);
PinTextBox.Text = person.Pin.ToString();
TitleComboBox.Text = person.Title;
LastNameTextBox.Text = person.LastName;
FirstNameTextBox.Text = person.FirstName;
FormerLastNameTextBox.Text = person.FormerLastName;
LastUpdatedTimeTextBox.Text = person.LastUpdateDateTime.ToShortDateString();
LastUpdatedUserTextBox.Text = person.LastUpdatedUser;
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message,
"People Registry",
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
finally
{
session.Flush();
session.Close();
session.Dispose();
}
}
Please advice me.
Thank You,
Nirupama.
nirupama wrote:
Hello Everyone,
I am new to Nhibernate and finding some problem using it..
Person is the name of my class and person_prsn is the name of the table. My project name is SHIFT. I am trying to query some data from the person_prsn table using below statement but I dont get anything in return.
IList personList = session.CreateCriteria(typeof(Person)).
Add(Expression.Eq("prsn_pin", PersonPin)).List();
I also tried using this:
IList personList = session.CreateQuery("from Person where Pin=?").SetInt32(0, person.Pin).List();
I get the following Exception:
"in expected: <end-of-text> (possibly an invalid or unmapped class name was used in the query)"
Anyone Plz help to solve this.
Thanks in Advance,
Nirupama.
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelpHibernate version: Mapping documents:Code between sessionFactory.openSession() and session.close():Full stack trace of any exception that occurs:Name and version of the database you are using:The generated SQL (show_sql=true):Debug level Hibernate log excerpt:Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html[code][/code]