-->
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.  [ 2 posts ] 
Author Message
 Post subject: One to Many for foreign key and composite-id
PostPosted: Wed May 14, 2008 6:47 am 
Newbie

Joined: Tue Aug 08, 2006 2:56 am
Posts: 6
Dear all, I hava a simple relationship for company and car.

companyVo.hbm.xml
<class name="emo.poc.model.vo.CompanyVo" table="company"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false">

<id name="uid" type="integer">
<generator class="increment" />
</id>

<property name="name" type="string" >
<column name="name" length="50" sql-type="NVARCHAR(50)" />
</property>
<set name="cars" lazy="false" cascade="all"
inverse="true" fetch="join">
<key column="compId" />
<one-to-many class="emo.poc.model.vo.CarVo"/>
</set>

</class>

Carvo.hbm.xml
<class name="emo.poc.model.vo.CarVo"
table="car"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false">

<composite-id>
<key-property name="compId" type="integer" />
<key-property name="carId" type="integer" />
</composite-id>

<property name="name" type="string" >
<column name="name" length="50" sql-type="NVARCHAR(50)" />
</property>

</class>

I want insert a company with its car into tables.
CompanyVo companyVo = new CompanyVo();
companyVo.setInsert(true);
companyVo.setName("n1");
CarVo carVo = new CarVo();
carVo.setCarId(1);
carVo.setName("cn");
Set cars = new HashSet();
cars.add(carVo);
carVo.setInsert(true);
companyVo.setCars(cars);
personBo.saveCompany(companyVo);

from the company's set(one-to-many) I have let the compId as a key, and its value should be same as company's id.
However,
after insert, they all insert one data in tables, but the uid of company is not set to car's compId, which mean car's compId is null, carId is 1...
Can some body tell me why, and please tell me how to let car's compId same as company's uid.

Thank you very much.[


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 9:47 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Are you setting both properties in the relationship, so calling the setter on both the owned, and the owning class? I'd bet that is not happening.

Also, have you set up a cascade type for the relationship? You might want to look at setting up a CascadeType.ALL setting somewhere, although I'm not sure if that would help in this particular scenario.

Here's a helpful tutorial on object relational mappings from Wikipedia:

http://en.wikipedia.org/wiki/Object-relational_mapping

Also, I have some free tutorials on implementing simple many-to-one mappings if you're interested:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=18mappingonetomanyassociations

-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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