-->
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.  [ 1 post ] 
Author Message
 Post subject: associations with foreign primary keys
PostPosted: Fri May 01, 2009 6:05 pm 
Newbie

Joined: Fri May 01, 2009 5:41 pm
Posts: 2
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


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

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.