-->
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.  [ 3 posts ] 
Author Message
 Post subject: Bidirectional one-to-many associations problems!
PostPosted: Mon Aug 01, 2005 10:17 pm 
Beginner
Beginner

Joined: Thu Jul 07, 2005 5:17 am
Posts: 30
Hi,all
parent table:sale_contract
child table:sale_bill
sale_contract's PK is cont_no,need to assign value manually(not identity or sequence)
sale_bill'FK is cont_no
child will control the association(inverse="true")
middlegen create mapping files:SaleContract.hbm.xml,SaleBill.hbm.xml
and pojos: SaleContract.java,SaleBill.java
that time,middlegen will add a SaleCntract attribute to replace the cont_no attribute automaticlly in SaleBill.java
question is there:when I insert a new record,I can't set the cont_no because there has not a setter/getter of cont_no attribute in the SaleBill.java

SaleContract contract=new SaleContract();//parent
SaleBill bill=new SaleBill();//child
//there is no bill.setContNo()
bill.setBillNo(new Long(77));
bill.setBookId("77");
....
bill.setContract(contract);
contract.getDetail().add(bill);

session.save(contract);
tx.commit();
session.close();

how can I do?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 02, 2005 2:49 pm 
Beginner
Beginner

Joined: Thu Jun 02, 2005 5:09 am
Posts: 22
You're thinking in terms of foreign keys, but you should think in terms of Object associations. Hibernate will set the cont_no column of the sale_bill table to the correct value, because you've called bill.setContract(contract). That is assuming that you're (generated) mappings are correct, of course.

The Java object is not supposed to have attributes that mirror the foreign key id's, it's supposed to be a proper object model that has associations (in this case, from a Bill to a Contract). This is basically how every O/R mapper works: try to read some more documentation, epecially the sample association mappings.

Joris


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 02, 2005 9:18 pm 
Beginner
Beginner

Joined: Thu Jul 07, 2005 5:17 am
Posts: 30
thank u for your suggestion,Joris,I think I've found the anwser.
I set the id field attribute 'unsaved-value=any' in the 2 mapping files, then is ok!


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