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
|