-->
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.  [ 7 posts ] 
Author Message
 Post subject: Composite ID & Many To One Join
PostPosted: Fri Jan 23, 2009 7:04 pm 
Newbie

Joined: Wed Jan 21, 2009 3:13 pm
Posts: 5
Location: New York, NY
Hi,

I need some assistance on this (seemingly) simple mapping:

Tables:
Code:
security
    - securityId
    - ... more columns


Code:
company_security
    - companyId
    - securityId
    - rank


CompanySecurity.hbm.xml:
Code:
<composite-id>
    <key-many-to-one name="company" column="companyId" class="Company"/>
    <key-many-to-one name="security" column="securityId" class="Security"/>
</composite-id>
<property name="rank" column="rank"/>


Security.hbm.xml:
Code:
<id name="securityId" column="securityId">
    <generator class="increment"/>
</id>
<join table="company_security" inverse="true">
    <key column="security_id"/>
    <many-to-one name="companySecurity" column="companyId" class="CompanySecurity"/>
</join>


The structure calls for many securities associated with a single company, but only one company associated with a particular security. I can get the one-to-many mapping fine for Company -> CompanySecurity but the Security -> CompanySecurity mapping gives me this exception:

Code:
org.hibernate.MappingException: Foreign key (FK4F9028C25CCF0F1B:company_security [companySecurity])) must have same number of columns as the referenced primary key (company_security [companyId,securityId])


We're working with a legacy database so I can't change structure. Could someone give a beginner some pointers?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 8:05 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Quote:
Security.hbm.xml:
Code:
<id name="securityId" column="securityId">
    <generator class="increment"/>
</id>
<join table="company_security" inverse="true">
    <key column="security_id"/>
    <many-to-one name="companySecurity" column="companyId" class="CompanySecurity"/>
</join>


I think you can not use a <join> as Security has a one-to-many-association to CompanySecurity. Try to use a set or a bag instead.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 10:56 am 
Newbie

Joined: Wed Jan 21, 2009 3:13 pm
Posts: 5
Location: New York, NY
Is there a way to have a set or bag be a single item? I would not want my Security object to have Set<CompanySecurity> because this is confusing. Thanks for the help!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 12:30 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
I do not really understand what you need. Does one security only have one company?? I thought, companies can have many securities (with a different rank, so you need company_security) and securities can have many companies. So why do you need one company per security?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 1:01 pm 
Newbie

Joined: Wed Jan 21, 2009 3:13 pm
Posts: 5
Location: New York, NY
Sorry I wasn't clear. A Company can have many CompanySecurity objects (I have this mapping done using a <set> and it works great) but a Security can only have one CompanySecurity object (this is the mapping I can't get right). It's similar to the Parent/Child relationship in the Hibernate docs but with a join table. Company is the parent and Security is the child in this case.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 1:09 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Ok, in that case you should not use a composite key in CompanySecurity.hbm.xml. If I understand your use case right, there is only one companySecurity per Security, so using only the securityId as primary key is the way to do it. So this relationship is actually a one-to-one(?)

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 11:39 am 
Newbie

Joined: Wed Jan 21, 2009 3:13 pm
Posts: 5
Location: New York, NY
Thanks for the help. That worked. However I still need to persist the company first then add the CompanySecurity object:

Code:
Company company = new Company(... params ...);
new CompanyDao().create(company);
company.addSecurity(new CompanySecurity(company.getCompanyId,security.getSecurityId(),1));
session.flush();


Minor issue that i can live with (or possibly figure out how to resolve). Thanks again for the help


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