-->
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 relation with composite key
PostPosted: Wed Jul 13, 2011 8:58 pm 
Newbie

Joined: Fri Apr 29, 2011 5:24 pm
Posts: 3
hi,
i have two tables with one to many relation. first table has composite key as primary key.

CandidatePositionAttachment table
PRIMARY KEY (candidateSeq, positionSeq, attachmentSeq)
FOREIGN KEY (attachmentSeq) REFERENCES CSS_Attachment

CSS_Attachment table
PRIMARY KEY attachmentSeq.

now how do I create mapping of one to many between candidatePositionAttachment and Attachment?

CandidatePositionAttachment.hbm.xml

Code:
<hibernate-mapping package="com.callidus.ssln.server.bean">
   
    <class name="CandidatePositionAttachment" table="CSS_CandidatePositionAttachment" lazy="false">
        <composite-id name="candidatePositionPK" class="com.callidus.ssln.server.bean.CandidatePositionPK">
           <key-property name="candidateSeq" column="candidateSeq" />
           <key-property name="positionSeq" column="positionSeq" />
           [b]<key-many-to-one[/b] name="attachmentSeq" column="attachmentSeq" class="Attachment"/>
        </composite-id>
         
        <property name="status"/>
        <property name="createDate"/>
       
       <!-- <many-to-one name="video" class="Attachment" column="attachmentSeq" not-null="true" cascade="all" lazy="false" unique="true"/> -->
    </class>

</hibernate-mapping>


is this correct? however it is not many to one mapping we have between these tables but it is one to many. and there is no key-one-to-many for this.

Requirement is to save multiple attachments for a given candidateId and positionId. using the above mapping, following code to save attachment for a given candidateId and positionId is giving null pointer exception when session.save is called. please point out what mistake i am doing.

Attachment videoAttachment = new Attachment();
CandidatePositionPK candidateJobPK = new CandidatePositionPK();
candidateJobPK.setCandidateSeq(1L);
candidateJobPK.setPositionSeq(1L);
candidateJobPK.setAttachmentSeq(videoAttachment);
CandidatePositionAttachment candidateJobVideo = new CandidatePositionAttachment();
candidateJobVideo.setId(candidateJobPK);
candidateJobVideo.setStatus("RECEIVED");
session.save(candidateJobVideo);


Top
 Profile  
 
 Post subject: Re: one to many relation with composite key
PostPosted: Fri Jul 15, 2011 1:23 pm 
Newbie

Joined: Fri Apr 29, 2011 5:24 pm
Posts: 3
as of now, i have sorted it out, to insert a record in child table(attachment here) and then get the id of record, set it as primary key of candidate, position, attachment. save it.
i wish there is a better way to handle it in hibernate way of doing. but with composite keys it is not allowing me to map attachment as object. since its id won't be available, it fails while saving candidate position attachment.


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.