Hello,
I am new to nHibernate. I have been working to merge nHibernate into
an existing code base. I have nHibernate configured and I can save,
update, delete from the db. I am working with a one class one table
model.

In my sqlserver I have a customer table with primary key (customerid)
and foreign keys to the following tables account, address and
emailaddress. I have individual c# classes for each table. I can
persist each class individually without issue.

My customer.hbm,xml is:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-
import="true">
  <class name="Com.Accounts.Customer, Com.Accounts" table="Customer"
lazy="false">
    <id name="CustomerID" column="CustomerID" type="Int32">
      <generator class="identity" />
    </id>
    <one-to-one name="EmailAddress" class="Com.Accounts.EmailAddress,
Com.Accounts"
    cascade="all"
    constrained="true"
    fetch="join"
    property-ref="EmailAddressID"
    access="property" />
    <one-to-one name="Address" class="Com.Accounts.Address,
Com.Accounts"
        cascade="all"
        constrained="true"
              fetch="join"
        property-ref="AddressID"
        access="property"/>
    <one-to-one name="Account" class="Com.Accounts.Account,
Com.Accounts"
        cascade="all"
        constrained="true"
              fetch="join"
        property-ref="AccountID"
        access="property"/>
  </class>
</hibernate-mapping>

When I persist the customer class the inserts for the one-to-ones
work. Debugging through the code I can see the primary keys being set
for the account, email and address objs.

8:10:48 PM
NHibernate: INSERT INTO EmailAddress (Email, IPAddress, IsActive)
VALUES (@p0, @
p1, @p2); select SCOPE_IDENTITY();@p0 = '[email protected]', @p1 =
'127.0.0.
1', @p2 = True
NHibernate: INSERT INTO Address (FullName, CorporationName,
AddressLine1, Addres
sLine2, AddressLine3, City, StateProvince, ZipCode, CountryID,
PhoneNumber, IPAd
dress, IsActive) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8,
@p9, @p10,
 @p11); select SCOPE_IDENTITY();@p0 = 'xxxx', @p1 = 'some corp', @p2 =
 '1 main street', @p3 = '', @p4 = '', @p5 = 'xxxxxx', @p6 = 'OH', @p7
= '27
520', @p8 = 1, @p9 = '111111111', @p10 = '127.0.0.1', @p11 = True
NHibernate: INSERT INTO Account (IPAddress, Password, IsActive,
AccountTypeID, A
ccountStatusID) VALUES (@p0, @p1, @p2, @p3, @p4); select SCOPE_IDENTITY
();@p0 =
'127.0.0.1', @p1 = 'password', @p2 = True, @p3 = Trial, @p4 = Active
NHibernate: INSERT INTO Customer DEFAULT VALUES; select SCOPE_IDENTITY
()

But the customer tbl insert fails since the foreign keys values are
null on the customer obj. How do I get the customer object to get the
new foreign key values from the one-to-one relationships? I though
Cascade=all would take care of it...

I have been googling but I can not see what I have done wrong.

Any help appreciated





--

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