-->
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.  [ 5 posts ] 
Author Message
 Post subject: Help with Composite key
PostPosted: Sun Apr 22, 2007 5:26 pm 
Newbie

Joined: Tue Apr 17, 2007 7:57 pm
Posts: 6
I can't figure this out.

I have a table, UserSites, which has a many-to-one relationship to Users.

i need the columns, "id" and "site_label" to be the composite primary key where "id" is a foreign key to Users.id and "site_label" is a user-defined site name that needs to be unique for a given user.

How do I set this up using annotations? Thanks in advance!


Top
 Profile  
 
 Post subject: Use either EmbeddedId or IdClass
PostPosted: Sun Apr 22, 2007 11:41 pm 
Newbie

Joined: Sun Apr 22, 2007 12:57 am
Posts: 7
There are two ways of doing it.

1. Use @IdClass

Code:
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.OneToMany;
import javax.persistence.ManyToOne;
import java.io.Serializable;
import java.util.Collection;
@Entity
public class User {
  public int id;

  @OneToMany(mappedBy = "user")
  public Collection<UserSite> sites;

  public static class UserSitePk implements Serializable {
    @ManyToOne
    public User user;

    public String name;
  }
 
  @Entity
  @IdClass(UserSitePk.class)
  public static class UserSite {
    @Id
    public User user;

    @Id
    public String name;
  }
}


2. Use @EmbeddedId and @Embeddable

Code:
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import java.io.Serializable;
import java.util.Collection;
@Entity
public class User {
  public int id;

  @OneToMany(mappedBy = "id.user")
  public Collection<UserSite> sites;

  @Embeddable
  public static class UserSitePk implements Serializable {
    @ManyToOne
    public User user;
    public String name;
  }

  @Entity
  public static class UserSite {
    @EmbeddedId
    public UserSitePk id;

  }
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 12:30 am 
Newbie

Joined: Tue Apr 17, 2007 7:57 pm
Posts: 6
Thank you so much sir. This is exactly what I needed.

Somebody should copy-paste this into the documentation :P


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 6:17 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If it's not in the doc, and if you provide a patch, I will apply it.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 24, 2007 12:58 am 
Newbie

Joined: Tue Apr 17, 2007 7:57 pm
Posts: 6
I think it's in the docs--just not explained so concisely as this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.