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: Hibernate Composite Primary Key
PostPosted: Sun Jan 10, 2010 11:55 pm 
Newbie

Joined: Tue Dec 15, 2009 3:33 am
Posts: 13
Location: Melbourne, Australia
Hello All,

Hope to get a bit of direction on coding a composite Primary Key.

I have a db table that three columns form a primary key (client_id, media_id, status).

This table maps to the following class:

Code:
@Entity
@Table(name = "media_syndicated_partner")
public class MediaSyndicatedPartner implements Serializable{
   
   private static final long serialVersionUID = 1L;
   
   @Id
        @GeneratedValue
   private Long id;
   
   @Enumerated(EnumType.STRING)
   private SyndicatedStatus status;
   
   @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
   @PrimaryKeyJoinColumn
        private Media media;
   
        @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
   @PrimaryKeyJoinColumn
         private Client client;

   public MediaSyndicatedPartner(){
        }

   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public SyndicatedStatus getStatus() {
      return status;
   }

   public void setStatus(SyndicatedStatus status) {
      this.status = status;
   }

   public Media getMedia() {
      return media;
   }

   public void setMedia(Media media) {
      this.media = media;
   }

   public Client getClient() {
      return client;
   }

   public void setClient(Client client) {
      this.client = client;
   }


I have a Media class:
Code:
@Entity
@Table(name = "media")
public class Media implements Serializable, Tagable{

   private static final long serialVersionUID = 1L;
   
   @Id
   @GeneratedValue
   @DocumentId
   private Long id;
   
   @NotNull
   @Enumerated(EnumType.STRING)
   private Status status;
   
   @NotNull
   @ManyToOne
   private Client client;
   
   @OneToMany(mappedBy = "media")
   @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   private List<MediaSyndicatedPartner> syndicatedPartners  = new ArrayList<MediaSyndicatedPartner>(0);

Getters and Setters ...


And a Client class:
Code:
@Entity
@Table(name = "client")
public class Client implements Serializable {

   private static final long serialVersionUID = 1L;
   
   @Id   
   @GeneratedValue
   private Long id;

   @OneToMany(mappedBy = "client")
   private List<Media> media;
   
        @OneToMany(mappedBy = "client")
   @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    private List<MediaSyndicatedPartner> syndicatedPartnerMedia;

Getters and Setters...


I have tried many combinations of creating a MediaSyndicatedParter Primary Key class which is embeddable. I have associated this class with MediaSyndicatedParter class with the @EmbeddedId annotation. I have had the @ManyToOne assocatitions moved into the Primary Key class and had the @OneToMany mappings indicate that mappedBy = pk.media or pk.client.

Either way, I can't seem to get the composite key to work properly. The closest I have gotten is that I'm able to save update a Media object (during a web service call) but when I try and serialise the Media object as XML (I need to get MediaSyndicatePartner.client object), this object is null.

Any help would be great in pointing me in the 'best practises' direction of setting up composite primary keys in Hibernate.

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.