Hi there,
I am fairly new to O/R Mapping and definately new to NHibernate. I have been searching through the documentation and these forums for a day or so, attempting to find a solution to my problem which is as follows.
I am attempting to create an example using NHibernate and the 'Pubs' database that ships with MSSQL2000.
I am trying to map the relation between Publishers and Titles in the following fashion:
Publishers:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernateParentChild.Publisher, NHibernateParentChild" table="Publishers">
<id name="PubId" column="pub_id" type="string" length="4" unsaved-value=" " >
<generator class="assigned" />
</id>
<set name="myTitles" cascade="all" >
<key column="pub_id" />
<one-to-many class="NHibernateParentChild.Titles, NHibernateParentChild"/>
</set>
<property name="PubName" column="Pub_Name" type="string" length="40"/>
<property name="City" column="city" type="string" length="20"/>
<property name="State" column="state" type="string" length="2"/>
<property name="Country" column="country" type="string" length="30"/>
</class>
</hibernate-mapping>
and Titles:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernateParentChild.Titles, NHibernateParentChild" table="Titles">
<id name="TitleId" column="title_id" type="string" length="6" unsaved-value=" " >
<generator class="assigned" />
</id>
<property name="Title" column="title" type="string" length="80" not-null="true"/>
<property name="Type" column="type" type="string" length="12" not-null="true"/>
<property name="Price" column="price" type="double" />
<property name="Advance" column="advance" type="double" />
<property name="Royalty" column="royalty" />
<property name="YTDSales" column="ytd_sales" />
<property name="Notes" column="notes" type="string" length="200"/>
<property name="PubDate" column="pubdate" type="DateTime" not-null="true"/>
<many-to-one name="pub" column="pub_id" class="NHibernateParentChild.Publisher, NHibernateParentChild" />
</class>
</hibernate-mapping>
My code for the Publishers class is as follows:
Code:
using System;
using System.Collections;
using System.Windows.Forms;
using NHibernate;
using NHibernate.Cfg;
namespace NHibernateParentChild
{
/// <summary>
/// Summary description for Publisher.
/// </summary>
public class Publisher
{
private string m_Pub_Id; //length 4
private string m_Pub_Name; //length 40
private string m_City; // length 20
private string m_State; // length 2
private string m_Country; // length 30
public string PubId
{
get {return m_Pub_Id;}
set{m_Pub_Id = value;}
}
public string PubName
{
get{return m_Pub_Name;}
set{m_Pub_Name = value;}
}
public string City
{
get{return m_City;}
set{m_City = value;}
}
public string State
{
get{return m_State;}
set{m_State = value;}
}
public string Country
{
get{return m_Country;}
set{m_Country = value;}
}
private IList m_titles;
public IList myTitles
{
get
{
if(m_titles == null)
{
m_titles = new ArrayList();
}
return m_titles;
}
set{m_titles = value;}
}
public Publisher()
{}
}
}
and my class for Titles looks like:
Code:
using System;
namespace NHibernateParentChild
{
/// <summary>
/// Summary description for Title.
/// </summary>
public class Titles
{
private string m_Title_Id;
private string m_Title;
private string m_Type;
private double m_Price;
private double m_Advance;
private int m_Royalty;
private int m_YTD_Sales;
private string m_Notes;
private DateTime m_PubDate;
private Publisher m_pub;
public string TitleId
{
get{return m_Title_Id;}
set{m_Title_Id = value;}
}
public string Title
{
get{return m_Title;}
set{m_Title = value;}
}
public string Type
{
get{return m_Type;}
set{m_Type = value;}
}
public double Price
{
get{return m_Price;}
set{m_Price = value;}
}
public double Advance
{
get{return m_Advance;}
set{m_Advance = value;}
}
public int Royalty
{
get{return m_Royalty;}
set{m_Royalty = value;}
}
public int YTDSales
{
get{return m_YTD_Sales;}
set{m_YTD_Sales = value;}
}
public string Notes
{
get{return m_Notes;}
set{m_Notes = value;}
}
public DateTime PubDate
{
get{return m_PubDate;}
set{m_PubDate = value;}
}
public Publisher pub
{
get{return m_pub;}
set{m_pub = value;}
}
public Titles()
{
}
}
}
When I attempted to load an instance of the Publisher object using the following code:
Code:
Configuration cfg = new Configuration();
cfg.AddAssembly("NHibernateParentChild");
Publisher pub = new Publisher();
Titles title = new Titles();
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
pub = (Publisher)session.Load(typeof(Publisher), "0736"); <= ****
I am getting an error back that says:
could not load object at NHibernate.Impl.SessionImpl.DoLoad(Type theClass, Object id, Object optionalObject, LockMode lockMode, Boolean checkDeleted)
The error is happening on the line marked with ****'s. If anyone has any ideas please let me know. I am having a bunch of trouble with this simple thing and I feel as though I must be missing something really silly :( [/list]