These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: One-to-one mapping fails to update child objects on insert.
PostPosted: Sun May 09, 2010 3:23 pm 
Newbie

Joined: Sun May 09, 2010 3:19 pm
Posts: 1
Hi,
I've being banging my head against the desk all day with the following Nhibernate problem.

Each bank account has one (and only one) set of rates associated with it. The primary key of the bank account table, BankAccountID is also the primary key of the AccountRates table.

public class BankAccount
{
public virtual int BankAccountId { get; set; }
public virtual string AccountName { get; set;}
public virtual AccountRate AccountRate {get;set;}
}

public class AccountRate
{
public virtual int BankAccountId { get; set; }
public virtual decimal Rate1 { get; set; }
public virtual decimal Rate2 { get; set; }
}

I have the following HBM mappings for BankAccount:

<class name="BankAccount" table="BankAccount">
<id name ="BankAccountId" column="BankAccountId">
<generator class="foreign">
<param name="property">
AccountRate
</param>
</generator>
</id>
<property name ="AccountName" column="AccountName" />
<one-to-one name="AccountRate" class="AccountRate" constrained="true" cascade="save-update"/>
</class>
and the following for AccountRate:

<class name="AccountRate" table="AccountRate">
<id name ="BankAccountId" column="BankAccountId">
<generator class="native" />
</id>
<property name ="Rate1" column="Rate1" />
<property name ="Rate2" column="Rate2" />
</class>
An existing BankAccount object can be read from the database with no problem. However, when a new BankAccount is created , the insert statement fails with;

Cannot insert the value NULL into column 'BankAccountId'
The issue appears to be that the child object , AccountRate is created first and since it hasn't yet got an identifier from its uncreated parent , the insert fails.

I think i'm correct in saying that if the AccountRate property on BankAccount was a collection i could use the following ?

Inverse=True
in order to force the parent to be inserted first.

Can anyone help me with this , i dont really want to use a collection as there is only a unidirectional one to one relationship between these tables.
Thanks

Paul


Top
 Profile  
 
 Post subject: Re: One-to-one mapping fails to update child objects on insert.
PostPosted: Tue May 11, 2010 4:01 am 
Beginner
Beginner

Joined: Fri Feb 27, 2009 6:07 am
Posts: 38
Location: Moscow,Russia
You write:
Quote:
AccountRate is created first and since it hasn't yet got an identifier from its uncreated parent.

but in your mapping files write that "BankAccount" must get an identifier from property "AccountRate".
When you save "BankAccount", property "AccountRate" must have an identifier.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.