-->
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.  [ 1 post ] 
Author Message
 Post subject: OneToMany with composite primary key not working
PostPosted: Tue Sep 27, 2011 12:44 am 
Newbie

Joined: Tue Sep 27, 2011 12:21 am
Posts: 1
Hello All - Wondering if someone can help me. Here's my setup briefly. I have a OneToMany between a Challenge and Participants. The challenge has a auto-increment PK id. The Participants have a composite primary key between an id (from challenge's PK) and user_email. Participants also has a FK to Challenge's id.

--------------------------
Challenge Table
id_challenge - PK Auto-Increment
--------------------------
Participants Table
id_challenge - PK
user_email - PK
fk_id - FK to Challenge.id
---------------------------
@IdClass(ChallengeParticipantPK.class)
public class Participants{
@ManyToOne()
@JoinColumn(name="id_challenge", insertable=false, updatable=false)
Challenge challenge;
@Id
@Column(name="id_challenge")
int id;
@Id
String email;
}
--------------------------
public class Challenge{
@OneToMany( cascade=CascadeType.ALL, mappedBy="id")
Set<ChallengeParticipant> participants;
}

---------------------------
When I run this, it does an insert into challenge (good), then a select from participants(BAD), then an insert into participants(good). I was thinking it should do the insert, then select from challenge to find the last UID inserted for challenge, then insert participants. Needless to say, I'm getting a foreign key constraint error on id_challenge in participant. Can anyone help? Is it a bad idea to have a composite primary key when you have a foreign key with one of those composite keys?

Any help would be appreciated.
Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.