I had a question about adding objects to a one to many association. I haven't found answers for this. I have a simple parent child relationship where a customer has many addresses. The address object has a customer object (foreign key). When I try to add an address into the database (ie. customer.addAddress) its giving me
Code:
a not-null property references a null or transient value: customer
. However, when I explicitly set the customer property on the address, then it adds it fine. I would assume that hibernate would do this automatically, is there something wrong with my setup?
Here is my Customer mapping:
Code:
<set name="addresses" inverse="false" lazy="true" table="ADDRESS" fetch="select" cascade="all">
<key>
<column name="CUSTOMER_ID" not-null="true" />
</key>
<one-to-many class="Address" />
</set>
</class>
</hibernate-mapping>
My address mapping:
Code:
<many-to-one name="customer" class="Customer" fetch="select">
<column name="CUSTOMER_ID" length="12" not-null="true" />
</many-to-one>
This doesn't work:
Code:
customer cust=dao.getCustomer();
x=new address();
cust.getaddresses.add(x);
(do not set the customer attribute )
This works:
Code:
customer cust=dao.getCustomer();
x=new address();
x.setCustomer(customer) ----->Why do I have to do this?
cust.getaddresses.add(x);
(do not set the customer attribute that I figured would happen automatically because the addresses collections is already part of that customer object)
Thanks in advance