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>