[jboss-user] [EJB 3.0] - Re: many to many mapping problem

jochen.reinhardt do-not-reply at jboss.com
Fri Mar 20 09:39:30 EDT 2009


Hi Ramu,

I suggest the following solution:
Split up your m:n relation into one additional entity and two 1:n relations.
Your additional entity could be named SkillLevel or something like that.

It's properties:
int SkillLevel

It has the following relations:
ManyToOne to Vendor
ManyToOne to Skill

The primary key must be a compound key -> skill, vendor.
Use an @Embeddable key class with @EmbeddedId that references these entities with their primary key.

Should look something like this: Please annotate the getters in your code...


  | @Entity
  | public class Vendor  implements Serializable
  | {
  |     @Id
  |     int id;
  |     @OneToMany(mappedBy="vendor")
  |     List<SkillLevel> skills;
  |     //...
  | }
  | 
  | @Entity
  | public class Skill  implements Serializable
  | {
  |     @Id
  |     int id;
  |     @OneToMany(mappedBy="skill")
  |     List<SkillLevel> skills;
  |     //...
  | }
  | 
  | @Entity
  | public class SkillLevel  implements Serializable
  | {
  |     @EmbeddedId
  |     SkillLevelKey key;
  |     int level;
  |     
  |     @ManyToOne
  |     Skill skill;
  | 
  |     @ManyToOne
  |     Vendor vendor;
  | 
  |     @Embeddable
  |     public static final class SkillLevelKey implements Serializable
  |     {
  |          int skill;
  |          int vendor;
  |          // must define equals() and hashCode()
  |     }
  | }
  | 

Hope that helps!

Cheers,
Jochen

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4219858#4219858

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4219858



More information about the jboss-user mailing list