-->
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.  [ 4 posts ] 
Author Message
 Post subject: identity not updated on one-to-many cascade..confusion???
PostPosted: Fri Feb 10, 2006 7:06 pm 
Beginner
Beginner

Joined: Thu Nov 03, 2005 4:11 pm
Posts: 25
I am having an issue with peristing one-to-manys I feel like i am missing something fundamental about how hibernate works. Maybe i am confused on the inverse mapping or something.

I have a situation where i have a parent Payment and then it can be assigned to many child account payments. When the payment is saved it should create/delete/update any of it's account payments. When i save the payment it cascades the save to the account payments in the database but the object identifier is not updated. If I save the account payment everything works. Naturally i have the inverse=true on the account_payments (many side).

The problem i am having is when i assign a payment to an account:
This code is from the payment class:

Quote:
public AccountPayment assignPaymentToAccount(double paymentAmount)
{
//if the payment isn't saved bomb out
if (this.id < 0)
return null;
if (paymentAmount > getUnassignedAmount())
return null;
AccountPayment returnValue = new AccountPayment(getCompanyId());
returnValue.setPayment(this);
returnValue.setAmount(paymentAmount);
accountPayments.add(returnValue);
return returnValue;
}



After I do this I call a saveOrUpdate on the Payment class. When I do that I expect the account payment id to be populated. The id is an identity(see my mappings below) but it isn't updated in the object. So i end up accessing it and it is an unsaved value which causes my program to crash. Wierdly the account payment is inserted with a proper identifier in my database, but it just isn't persisted back to the object. There is some timing issue or something.

my code is like this:

Quote:
1)transaction start;
2)AccountPayment newAccountPayment = assignPaymentToAccount(amount);
3)saveOrUpdate(payment);
4)do something with newAccountPayment.getId(); //CRASHES system because the id is still an unsaved value
5)transaction stop;


The accountpayment id is not being set for some reason so my system crashes. But it is saved properly in the db. Not only is it not set on the AccountPayment object returned i also checked the accountPayments set of the payment object and it isn't set there either. Now if I save the account payment instead of the payment it works. Like so:

Quote:
1)transaction start;
2)AccountPayment newAccountPayment = assignPaymentToAccount(amount);
3)saveOrUpdate(newAccountPayment);
4)do something with newAccountPayment.getId(); //WORKS!
5)transaction stop;


Why is that??????????????????

Below are my hibernate mappings:
Payment:
Quote:
<hibernate-mapping auto-import="true">
<class name="Payment" table="PAYMENT" >
<id name="id" column="ID" type="int" unsaved-value="-1" access="field">
<generator class="identity"/>
</id>
<property name="date" column="DATE" type="timestamp" not-null="true"/>
<property name="amount" column="AMOUNT" not-null="true"/>
<set name="accountPayments" cascade="all-delete-orphan" inverse="true" access="field">
<key column="PAYMENT_ID"/>
<one-to-many class="AccountPayment"/>
</set>
</class>
</hibernate-mapping>


Account Payment:
Quote:
<hibernate-mapping auto-import="true">
<class name="AccountPayment" table="ACCOUNT_PAYMENT">
<id name="id" column="ID" type="int" unsaved-value="-1" access="field">
<generator class="identity"/>
</id>
<property name="amount" column="AMOUNT" not-null="true"/>
<many-to-one
name="payment"
class="Payment"
column="PAYMENT_ID"
update="false"
cascade="save-update"
not-null="true"/>
</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 7:59 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
I am having the same problem.. my parent object has a many to many to its child objects (using a join table), and is uni directional (parent->child), when I save the parent the unsaved children are persisted to the database, but their identifiers are never updated, even though the sql IS calling "Hibernate: call identity()". Is there something we are failing to do here? Is this a known bug?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 8:41 am 
Newbie

Joined: Mon Jun 26, 2006 3:20 pm
Posts: 15
Can anyone help?? This is the error I'm getting too!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 10:40 am 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
I ended up making it bi-directional, and setting inverse=true on the parent, setting the child's parent object to the parent and saving it, and everything works fine from that end of things.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.