-->
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: Defining composite primary key
PostPosted: Wed May 11, 2011 9:57 am 
Newbie

Joined: Mon May 09, 2011 11:50 am
Posts: 8
Hi,

I've following class structure:
Code:
public class MainClass {
    @Id
    @Column(name="MAIN_ID")
    private Long id;

    @OneToOne(optional=false)
    @PrimaryKeyJoinColumn   
    private PartOne part;

}

public class PartOne{
    @Id
    @Column(name="MAIN_ID")
    private Long mainId;

   //other properties   
}

public class PartOneDetails {
     private Long partOneId;
}


Now, relationship until MainClass and PartOne is fine. However, now I want to make relationship from PartOne to PartOneDetails. Where PartOneDetails will have its own primary key and a foreign key to PartOne.

How should I do this???


Top
 Profile  
 
 Post subject: Re: Defining composite primary key
PostPosted: Wed May 11, 2011 11:40 am 
Newbie

Joined: Mon May 09, 2011 11:50 am
Posts: 8
Issue resolved! Not sure if this is the best way if anyone has any comments or suggestion please advice.

Relationship between PartOne and PartOneDetails is defined as follows:
Code:
public class MainClass {
    @Id
    @Column(name="MAIN_ID")
    private Long id;

    @OneToOne(optional=false)
    @PrimaryKeyJoinColumn   
    private PartOne part;

}

public class PartOne{
    @Id
    @Column(name="MAIN_ID")
    private Long mainId;

    @OneToMany(cascade=CascadeType.ALL,mappedBy="mainId")
    public List<PartOneDetails> getPartDetails() {
      return partDetails;
   }
   //other properties   
}

public class PartOneDetails {
     @Id
     private Long partOneId;
     
     @ManyToOne(targetEntity=PartOne.class)
     @JoinColumn(name="MAIN_ID", nullable=false)
     @ForeignKey(name="FK_PART_ONE_DETAILS_PART_ONE")
     public Long getMainId() {
   return mainId;
     }     
}


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.