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.