-->
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: JPA - One-to-many relationship with composite primary key
PostPosted: Thu Jul 23, 2009 10:36 am 
Newbie

Joined: Thu Jul 23, 2009 10:11 am
Posts: 2
Hello Everyone,

I am trying to create a one-to-many uni-directional relationship between a 'User' entity and a 'PhoneNumber' entity with 'User' at the owning side of the relationship. The 'User' Entity has a compound primary key consisting of a 'username' and 'bsn'.
Code:
public class User implements java.io.Serializable
{
    private UserPrimaryKey userPk; // Embedding primary key class directly into the bean class.
    private String firstName, middleName, lastName;
    ....
    // Defining one-to-many unidirectional relationship with phone numbers
    private List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();
    ....
    public List<PhoneNumber> getPhoneNumbers()
    { return phoneNumbers;}
    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers)
    {this.phoneNumbers = phoneNumbers;}
}


Here's the code snippet from orm.xml

Code:
<entity class="nl.myCompany.domain.User" access="PROPERTY">
   <attributes>
            <embedded-id name="userPk"/>
            .....
            <one-to-many name="phoneNumbers" target-entity="nl.igz.instellingsrapporten.domain.PhoneNumber" fetch="LAZY">
                <join-table name="USER_PHONE_NUMBERS">
                    <join-column name="userPk"/> <!-- Foreign key mapping to the primary key of the owning side of relationship -->
                    <inverse-join-column name="PHONENUMBER_ID"/> <!-- Non owning side of the relationship -->
                </join-table>
                <cascade><cascade-all/></cascade>
            </one-to-many>
            <transient name="userName"/>
            <transient name="bsn"/>
   </attributes>
</entity>
    <entity class="nl.myCompany.domain.PhoneNumber" access="PROPERTY">
        <attributes>
            <id name="id">
                <generated-value strategy="AUTO"/>
            </id>
            <!--
                The persistence provider will assume any other property (getters-setters in javabeans style) in class
                is a persisitence property and map them based on their based name and type.
            -->
        </attributes>
    </entity>
    <embeddable class="nl.myCompany.domain.UserPrimaryKey" access="PROPERTY">
        <description>
            Identify the persistent properties of the embeddable class.
            These columns don't change hence should not be included in SQL UPDATE statements.
            Make updatable as false.
        </description>
        <attributes>
            <basic name="bsn" optional="false">
                <column name="BSN" nullable="false" updatable="false" unique="true" length="9"/>
            </basic>
            <basic name="loginId">
                <column name="LOGIN_ID" nullable="false" updatable="false" unique="true" length="25"/>
            </basic>
        </attributes>
    </embeddable>


As I deploy the ejb 3 jar into jboss application server (version 4.2.3 GA), I encounter the following error.

org.hibernate.AnnotationException: A Foreign key refering nl.myCompany.domain.User from nl.myCompany.domain.PhoneNumber has the wrong number of column. should be 2

Any help in this regards would be highly appreciated.

Thanks a lot for your time and efforts in advance.

V


Top
 Profile  
 
 Post subject: Re: JPA - One-to-many relationship with composite primary key
PostPosted: Thu Jul 23, 2009 10:49 am 
Newbie

Joined: Thu Jul 23, 2009 10:11 am
Posts: 2
Making the following change to the orm.xml solves the problem.
Code:
          <one-to-many name="phoneNumbers" target-entity="nl.igz.instellingsrapporten.domain.PhoneNumber" fetch="LAZY">
                <join-table name="USER_PHONE_NUMBERS">
                    <join-column name="bsn"/>
                    <join-column name="userName"/>
                    <!-- Foreign key mapping to the primary key of the owning side of relationship -->
                    <inverse-join-column name="PHONENUMBER_ID"/> <!-- Non owning side of the relationship -->
                </join-table>
                <cascade><cascade-all/></cascade>
          </one-to-many>


It seems to me that, even though the java code uses embedded compound primary key, the individual fields in the primary key class must be listed explicitly instead of the primary key class itself.

Is it the only approach to tackle the error previously encountered ? I would prefer to use a reference to the primary key class instead. Is there a way to achieve this ?


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.