Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 1.20 CR1
Mapping documents:
Client.hbm.xml - The 'Parent' Class
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="SocksCDS.Client,SocksCDS" table="Client" lazy="true">
<id name="Clientid" column="ClientID" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property column="ClientSSN" type="String" name="Clientssn" not-null="true" length="50" />
<property column="LastName" type="String" name="LastName" length="50" />
<property column="FirstName" type="String" name="FirstName" length="50" />
<property column="Street" type="String" name="Street" length="50" />
<property column="City" type="String" name="City" length="50" />
<property column="State" type="String" name="State" length="2" />
<property column="Zipcode" type="String" name="Zipcode" length="50" />
<property column="PhoneNumber" type="String" name="PhoneNumber" length="50" />
<property column="Sex" type="String" name="Sex" length="10" />
<property column="Race" type="String" name="Race" length="50" />
<property column="MaritalStatus" type="String" name="MaritalStatus" length="20" />
<property column="AuthorizedPerson1" type="String" name="AuthorizedPerson1" length="50" />
<property column="AuthorizedPerson2" type="String" name="AuthorizedPerson2" length="50" />
<property column="ApplicationDate" type="DateTime" name="ApplicationDate" />
<property column="MI" type="String" name="Mi" length="1" />
<property column="AgeGroup" type="String" name="AgeGroup" length="50" />
<set name="Children" inverse="true">
<key column="Clientid" />
<one-to-many class="SocksCDS.Children,SocksCDS" />
</set>
<set name="Assistance" inverse="true">
<key column="Clientid" />
<one-to-many class="SocksCDS.Assistance,SocksCDS" />
</set>
</class>
</hibernate-mapping>
The 'child' Class (A Many part of the One-To-Many Relationship)Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="SocksCDS.Assistance,SocksCDS" table="Assistance" lazy="true">
<id name="Assistanceid" column="AssistanceID" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property column="TypeOfAssistance" type="String" name="TypeOfAssistance" length="50" />
<property column="DateOfAssistance" type="DateTime" name="DateOfAssistance" />
<property column="Amount" type="Decimal" name="Amount" />
<many-to-one name="Clientid" column="ClientID" class="SocksCDS.Client,SocksCDS" />
<property column="ConfirmationNumber" type="String" name="ConfirmationNumber" length="50" />
</class>
</hibernate-mapping>
Appropriate Class Files:
Client ClassCode:
using System;
using System.Collections;
using System.Collections.Generic;
using Iesi.Collections.Generic;
namespace SocksCDS
{
[Serializable]
public class Client
{
#region Private Members
private bool isChanged;
private bool isDeleted;
private int clientid;
private string clientssn;
private string lastname;
private string firstname;
private string street;
private string city;
private string state;
private string zipcode;
private string phonenumber;
private string sex;
private string race;
private string maritalstatus;
private string authorizedperson1;
private string authorizedperson2;
private DateTime applicationdate;
private string mi;
private string agegroup;
private Iesi.Collections.ISet children;
[b]private Iesi.Collections.ISet assistance;[/b]
#endregion
#region Default ( Empty ) Class Constuctor
public Client()
{
clientid = 0;
clientssn = null;
lastname = null;
firstname = null;
street = null;
city = null;
state = null;
zipcode = null;
phonenumber = null;
sex = null;
race = null;
maritalstatus = null;
authorizedperson1 = null;
authorizedperson2 = null;
applicationdate = DateTime.MinValue;
mi = null;
agegroup = null;
}
#endregion // End of Default ( Empty ) Class Constuctor
#region Public Properties
public virtual Iesi.Collections.ISet Children
{
get { return children; }
set { children = value; }
}
[b]public virtual Iesi.Collections.ISet Assistance
{
get { return assistance; }
set { assistance = value; }
}[/b]
#region Equals And HashCode Overrides
/// <summary>
/// local implementation of Equals based on unique value members
/// </summary>
public override bool Equals( object obj )
{
if( this == obj ) return true;
if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
Client castObj = (Client)obj;
return ( castObj != null ) &&
( this.clientid == castObj.Clientid );
}
/// <summary>
/// local implementation of GetHashCode based on unique value members
/// </summary>
public override int GetHashCode()
{
int hash = 57;
hash = 27 * hash * clientid.GetHashCode();
return hash;
}
#endregion
}
}
Assistance.cs Class FileCode:
/*
insert license info here
*/
using System;
namespace SocksCDS
{
/// <summary>
/// Generated by MyGeneration using the NHibernate Object Mapping adapted by Gustavo
/// </summary>
[Serializable]
public class Assistance
{
#region Private Members
private bool isChanged;
private bool isDeleted;
private int assistanceid;
private string typeofassistance;
private DateTime dateofassistance;
private decimal? amount;
private Client clientid;
private string confirmationnumber;
#endregion
#region Default ( Empty ) Class Constuctor
/// <summary>
/// default constructor
/// </summary>
public Assistance( )
{
assistanceid = 0;
typeofassistance = null;
dateofassistance = DateTime.MinValue;
amount = null;
clientid = null;
confirmationnumber = null;
}
#endregion // End of Default ( Empty ) Class Constuctor
#region Public Properties
/// <summary>
///
/// </summary>
public virtual int Assistanceid
{
get { return assistanceid; }
set { isChanged |= (assistanceid != value); assistanceid = value; }
}
/// <summary>
///
/// </summary>
public virtual string TypeOfAssistance
{
get { return typeofassistance; }
set
{
if ( value != null)
if( value.Length > 50)
throw new ArgumentOutOfRangeException("Invalid value for TypeOfAssistance", value, value.ToString());
isChanged |= (typeofassistance != value); typeofassistance = value;
}
}
/// <summary>
///
/// </summary>
public virtual DateTime DateOfAssistance
{
get { return dateofassistance; }
set { isChanged |= (dateofassistance != value); dateofassistance = value; }
}
/// <summary>
///
/// </summary>
public virtual decimal? Amount
{
get { return amount; }
set { isChanged |= (amount != value); amount = value; }
}
/// <summary>
///
/// </summary>
public virtual Client Clientid
{
get { return clientid; }
set { isChanged |= (clientid != value); clientid = value; }
}
/// <summary>
/// Hidden in case the customer ever wants it.
/// </summary>
public virtual string ConfirmationNumber
{
get { return confirmationnumber; }
set
{
if ( value != null)
if( value.Length > 50)
throw new ArgumentOutOfRangeException("Invalid value for ConfirmationNumber", value, value.ToString());
isChanged |= (confirmationnumber != value); confirmationnumber = value;
}
}
/// <summary>
/// Returns whether or not the object has changed it's values.
/// </summary>
public virtual bool IsChanged
{
get { return isChanged; }
}
/// <summary>
/// Returns whether or not the object has changed it's values.
/// </summary>
public virtual bool IsDeleted
{
get { return isDeleted; }
}
#endregion
#region Public Functions
/// <summary>
/// mark the item as deleted
/// </summary>
public virtual void MarkAsDeleted()
{
isDeleted = true;
isChanged = true;
}
#endregion
#region Equals And HashCode Overrides
/// <summary>
/// local implementation of Equals based on unique value members
/// </summary>
public override bool Equals( object obj )
{
if( this == obj ) return true;
if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
Assistance castObj = (Assistance)obj;
return ( castObj != null ) &&
( this.assistanceid == castObj.Assistanceid );
}
/// <summary>
/// local implementation of GetHashCode based on unique value members
/// </summary>
public override int GetHashCode()
{
int hash = 57;
hash = 27 * hash * assistanceid.GetHashCode();
return hash;
}
#endregion
}
}
Code between sessionFactory.openSession() and session.close():Code:
private void btnAddAssistance_Click(object sender, EventArgs e)
{
Configuration cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly("SocksCDS");
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
Client client = session.Get<Client>(int.Parse(txtClientID.Text));
Assistance a = new Assistance();
a.DateOfAssistance = Convert.ToDateTime(TxtAssistanceDate.Text);
a.TypeOfAssistance = comboTypeOfAssistance.Text;
a.Amount = Convert.ToDecimal(txtAmount1.Text);
client.Assistance.Add(a);
session.Save(a);
transaction.Commit();
session.Flush();
session.Close();
}
Full stack trace of any exception that occurs:
No Exception Occurs. Saves Object fine, it just doesn't put the Parents.PK in the Child.FK field.
Name and version of the database you are using:
Access 2003
The issue is that I can save an 'assistance' object fine -- but I can't get it to link it to the Parent object in the DB like it should. In the Assistance.ClientID field (in the DB) it should have the Foreign Key from Client.ClientID -- it doesn't. (In fact, it has nothing in there).
EDIT: I left out some of the stuff from the Client.cs file on purpose, to make it shorter for consumption.