Guys, I need help on this. I can't seem to find any solution to this.

I'm trying to insert records to 3 classes/tables. However, the error
occurred when the records are about to be inserted.

I'm on a complete dead-end on this case. Below are the classes and the
mapping files

static void Main(string[] args)
        {
            Configuration cfg = new Configuration();


 cfg.Properties.Add(NHibernate.Cfg.Environment.ConnectionProvider,
                typeof(NHibernate.Connection.DriverConnectionProvider)
                .AssemblyQualifiedName);

            cfg.Properties.Add(NHibernate.Cfg.Environment.Dialect,
                typeof(NHibernate.Dialect.MsSql2008Dialect)
                .AssemblyQualifiedName);

            cfg.Properties.Add(NHibernate.Cfg.Environment.ConnectionDriver,
                typeof(NHibernate.Driver.SqlClientDriver)
                .AssemblyQualifiedName);

            cfg.Properties.Add(NHibernate.Cfg.Environment.ConnectionString,
                @"Data Source=DBW-BSG-N87\sqlexpress;Initial
Catalog=NHibernateDB;Integrated Security=True");


 cfg.Properties.Add(NHibernate.Cfg.Environment.ProxyFactoryFactoryClass,

 typeof(NHibernate.ByteCode.Castle.ProxyFactoryFactory).AssemblyQualifiedName);

            cfg.AddAssembly(typeof(Address).Assembly);

            //Create Session Factory
            ISessionFactory sessionFactory = cfg.BuildSessionFactory();

            //Create Session and Transaction
            ISession session = sessionFactory.OpenSession();
            ITransaction tx = session.BeginTransaction();

            //Insert records
            //Contact contact = new Contact("Brando", "Washington", "
[email protected]");
            //session.Save(contact);
            //tx.Commit();

            Contact ordCntct = new Contact("Martha", "Washington", "
[email protected]");

            Address ordAddr = new Address("1600 Pennsylvania Ave, NW", null,
"Washington, DC", "20500");
            ordAddr.AddressContact = ordCntct;
            ordCntct.ContactAddress = new List<Address>();
            ordCntct.ContactAddress.Add(ordAddr);

            OrderHeader header = new OrderHeader();
            header.Number = "0000001";
            header.OrderDate = DateTime.Now;
            header.BillToContact = ordCntct;
            header.BillToAddress = ordAddr;
            header.ShipToContact = ordCntct;
            header.ShipToAddress = ordAddr;

            //OrderHeader header = new OrderHeader("00001", DateTime.Now,
-1, -1, ordCntct, ordCntct, ordAddr, ordAddr);
            session.SaveOrUpdate(header);
            tx.Commit();
        }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ordering.Data
{
    public class Contact
    {
        #region Properties
        private int _ID;
        private string _lastName;
        private string _firstName;
        private string _email;
        private IList<Phone> _contactPhone;
        private IList<Address> _contactAddress;
        private IList<OrderHeader> _billToContacts;
        private IList<OrderHeader> _shipToContacts;

        public virtual IList<OrderHeader> ShipToContacts
        {
            get { return _shipToContacts; }
            set { _shipToContacts = value; }
        }

        public virtual IList<OrderHeader> BillToContacts
        {
            get { return _billToContacts; }
            set { _billToContacts = value; }
        }

        public virtual IList<Address> ContactAddress
        {
            get { return _contactAddress; }
            set { _contactAddress = value; }
        }

        public virtual IList<Phone> ContactPhone
        {
            get { return _contactPhone; }
            set { _contactPhone = value; }
        }

        public virtual string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        public virtual string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        public virtual string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        public virtual int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        #endregion

        #region Constructor
        public Contact()
        { }

        public Contact(string lastName, string firstName, string email)
        {
            this.LastName = lastName;
            this.FirstName = firstName;
            this.Email = email;
        }
        #endregion
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ordering.Data
{
    public class Address
    {
        #region Attributes and Properties
        private int _ID;
        private string _address1;
        private string _address2;
        private string _city;
        private string _zipCode;
        private Contact _addressContact;
        private IList<OrderHeader> billToAddress;
        private IList<OrderHeader> shipToAddress;

        public virtual IList<OrderHeader> ShipToAddress
        {
            get { return shipToAddress; }
            set { shipToAddress = value; }
        }

        public virtual IList<OrderHeader> BillToAddress
        {
            get { return billToAddress; }
            set { billToAddress = value; }
        }

        public virtual Contact AddressContact
        {
            get { return _addressContact; }
            set { _addressContact = value; }
        }

        public virtual string ZipCode
        {
            get { return _zipCode; }
            set { _zipCode = value; }
        }

        public virtual string City
        {
            get { return _city; }
            set { _city = value; }
        }

        public virtual string Address2
        {
            get { return _address2; }
            set { _address2 = value; }
        }

        public virtual string Address1
        {
            get { return _address1; }
            set { _address1 = value; }
        }

        public virtual int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        #endregion

        #region Constructors
        public Address()
        { }

        /// <summary>
        ///
        /// </summary>
        /// <param name="address1"></param>
        /// <param name="address2"></param>
        /// <param name="city"></param>
        /// <param name="zipCode"></param>
        public Address(string address1,
            string address2,
            string city,
            string zipCode)
            : this()
        {
            this.Address1 = address1;
            this.Address2 = address2;
            this.City = city;
            this.ZipCode = zipCode;
        }
        #endregion
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ordering.Data
{
    public class OrderHeader
    {
        #region Properties and Attributes
        private int _ID;
        private string _number;
        private DateTime _orderDate;
        private int _itemQty;
        private decimal _total;
        private Contact _billToContact;
        private Contact _shipToContact;
        private Address _billToAddress;
        private Address _shipToAddress;
        private IList<OrderItem> _headerItems;

        public virtual IList<OrderItem> HeaderItems
        {
            get { return _headerItems; }
            set { _headerItems = value; }
        }

        public virtual DateTime OrderDate
        {
            get { return _orderDate; }
            set { _orderDate = value; }
        }

        public virtual Address ShipToAddress
        {
            get { return _shipToAddress; }
            set { _shipToAddress = value; }
        }

        public virtual Address BillToAddress
        {
            get { return _billToAddress; }
            set { _billToAddress = value; }
        }

        public virtual Contact ShipToContact
        {
            get { return _shipToContact; }
            set { _shipToContact = value; }
        }

        public virtual Contact BillToContact
        {
            get { return _billToContact; }
            set { _billToContact = value; }
        }

        public virtual decimal Total
        {
            get { return _total; }
            set { _total = value; }
        }

        public virtual int ItemQty
        {
            get { return _itemQty; }
            set { _itemQty = value; }
        }

        public virtual string Number
        {
            get { return _number; }
            set { _number = value; }
        }

        public virtual int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        #endregion

        #region Constructors
        public OrderHeader()
        { }

        public OrderHeader(string number, DateTime orderDate, int itemQty,
decimal total,
                            Contact billToContact, Contact shipToContact,
                            Address billToAddress,
                            Address shipToAddress)
        {
            this.Number = number;
            this.OrderDate = orderDate;
            this.ItemQty = itemQty;
            this.Total = total;
            this.BillToContact = billToContact;
            this.ShipToContact = shipToContact;
            this.BillToAddress = billToAddress;
            this.ShipToAddress = shipToAddress;
        }
        #endregion

    }
}

(Filename: Contact.hbm.xml)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="Ordering.Data"
                   assembly="Ordering.Data">
  <class name="Ordering.Data.Contact, Ordering.Data"
         table="Contact">
    <id name="ID">
      <column name="ID" sql-type="int"/>
      <generator class="native"/>
    </id>

    <property name="LastName" type="String"/>
    <property name="FirstName" type="String"/>
    <property name="Email" type="String"/>

    <!--Many to Many Relationship-->


    <!--One to Many Relationship-->
    <bag name="BillToContacts" inverse="true"
         lazy="true" cascade="all-delete-orphan">
      <key column="BillToContact_ID"/>
      <one-to-many class="OrderHeader"/>
    </bag>

    <bag name="ShipToContacts" inverse="true"
         lazy="true" cascade="all-delete-orphan">
      <key column="ShipToContact_ID"/>
      <one-to-many class="OrderHeader"/>
    </bag>

    <bag name="ContactAddress" inverse="true"
             lazy="true" cascade="all-delete-orphan">
      <key column="Contact_ID"/>
      <one-to-many class="Address"/>
    </bag>

    <bag name="ContactPhone" table="Contact_Phone" inverse="false"
         lazy="true" cascade="none">
      <key>
        <column name="Contact_ID" length="4" sql-type="Int32"
not-null="true"/>
      </key>

      <many-to-many class="Phone">
        <column name="Phone_ID" length="4" sql-type="Int32"
not-null="true"/>
      </many-to-many>
    </bag>

  </class>
</hibernate-mapping>

(Filename: Address.hbm.xml)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="Ordering.Data"
                   assembly="Ordering.Data">
  <class name="Ordering.Data.Address, Ordering.Data"
         table="Address">
    <id name="ID">
      <column name="ID" sql-type="Int32"/>
      <generator class="native"/>
    </id>

    <property name="Address1" type="String" not-null="true"/>
    <property name="Address2" type="String" not-null="false"/>
    <property name="City" type="String" not-null="true"/>
    <property name="ZipCode" type="String" not-null="false"/>

    <!--Many to One relationship-->
    <many-to-one name="AddressContact" class="Contact">
      <column name="Contact_ID" length="4" sql-type="Int32"/>
    </many-to-one>

    <!--One to Many relationship-->
    <bag name="BillToAddress" inverse="true"
         lazy="true" cascade="all-delete-orphan">
      <key column="BillToAddress_ID"/>
      <one-to-many class="Address"/>
    </bag>

    <bag name="ShipToAddress" inverse="true"
         lazy="true" cascade="all-delete-orphan">
      <key column="ShipToAddress_ID"/>
      <one-to-many class="Address"/>
    </bag>

  </class>
</hibernate-mapping>

(Filename: OrderHeader.hbm.xml)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="Ordering.Data"
                   assembly="Ordering.Data">
  <class name="Ordering.Data.OrderHeader, Ordering.Data"
         table="OrderHeader">
    <id name="ID">
      <column name="ID" sql-type="Int32"/>
      <generator class="native"/>
    </id>

    <property name="Number" type="String"/>
    <property name="OrderDate" type="DateTime"/>
    <property name="ItemQty" type="Int32"/>
    <property name="Total" type="Decimal"/>

    <!--Many to One relationship-->
    <many-to-one name="BillToContact" class="Contact">
      <column name="BillToContact_ID" length="4" sql-type="Int32"/>
    </many-to-one>

    <many-to-one name="ShipToContact" class="Contact">
      <column name="ShipToContact_ID" length="4" sql-type="Int32"/>
    </many-to-one>

    <many-to-one name="BillToAddress" class="Contact">
      <column name="BillToAddress_ID" length="4" sql-type="Int32"/>
    </many-to-one>

    <many-to-one name="ShipToAddress" class="Contact">
      <column name="ShipToAddress_ID" length="4" sql-type="Int32"/>
    </many-to-one>

    <!--One to Many relationship-->
    <bag name="HeaderItems" inverse="true"
         lazy="true" cascade="all-delete-orphan">
      <key column="OrderHeader_ID"/>
      <one-to-many class="OrderItem"/>
    </bag>

  </class>
</hibernate-mapping>

Please help me. I'm really at a dead end on this.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to