-->
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: Many-to-Many Collection Mapping Issue
PostPosted: Tue Jun 24, 2008 11:17 pm 
Newbie

Joined: Thu Jan 31, 2008 5:32 pm
Posts: 2
I'm having trouble trying to create a hibernate mapping file for my application. The POJOs that I coded consist of Employee, Skill, Skill Level, and EmployeeSkill. I would like to code a method in the Employee POJO called getEmployeeSkills() which returns a collection of EmployeeSkill objects (maps skills with skill level to an employee). Given the scenario that I just described, how would I best design the Employee.hbm.xml? I really have not idea how to go about doing this. The EMPLOYEE_SKILL table is not your traditional join table (joining one table to another.) It joins one table (employee table) to 2 tables (skill and skill level tables.) Employee has a many to many relationship will skill and also skill_level, but the relationship should be skill and skill level together. I don't think <many-to-many> will work for what I just described. Can someone please help? I've been struggling with this for days and am about to give up on hibernate and just use JDBC.

Thanks in advanced!

Code:
CREATE TABLE EMPLOYEE
(
   id int primary key,
   name varchar(255) not null,
);

INSERT INTO EMPLOYEE(id, name) values (1, 'John Smith');
INSERT INTO EMPLOYEE(id, name) values (2, 'Jane Smith');

create table skill
(
   id int primary key,
   skill varchar(255) not null
);

INSERT INTO SKILL(id, skill) values (1, 'Java');
INSERT INTO SKILL(id, skill) values (2, 'C');

create table skill_level
(
   id int primary key,
   skill_level varchar(255) not null
);

INSERT INTO skill_level(id, skill_level) values (1, 'Beginner');
INSERT INTO skill_level(id, skill_level) values (2, 'Intermediate');
INSERT INTO skill_level(id, skill_level) values (3, 'Expert');

create table employee_skill
(
   id int primary key,
   employee_id int not null,
   skill_id int not null,
   skill_level_id int not null,
        foreign key(employee_id) references employee(id),
   foreign key(skill_id) references skill(id),
   foreign key(skill_level_id) references skill_level(id)
);

INSERT INTO employee_skill(id, employee_id, skill_id, skill_level_id) values (1, 1, 1, 1);
INSERT INTO employee_skill(id, employee_id, skill_id, skill_level_id) values (2, 2, 2, 3);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 25, 2008 10:12 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
You're scenario is actually pretty close to the one I outline on my website. Ignore the pointed arrows that actually should be association lines (Darn IRAD!), and you can see the various one-to-one, one-to-many and many-to-many relationships that get mapped.

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=20advancedentitymapping

Check out the code on the site. The classes use annotations, but the mapping files have very similar corollary. I think you'll find it will really guide you in what you are trying to accomplish.

Image

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=20advancedentitymapping

Code:
  package com.examscam.model;
  import java.util.*;import javax.persistence.*;
  @Entity
  @Table(name = "client", schema = "examscam")
  public class Client {
  private List<Address> addresses = new Vector<Address>();
  private List<Skill> skills = new Vector<Skill>();
  private ClientDetail clientDetail;
  private Long id;private String username;
  private String password;private Boolean verified;
  @Id
  @GeneratedValue
  @Column(name = "id")
  public Long getId() {return id;}
  public void setId(Long id) {this.id = id;}
  @ManyToMany
  @JoinTable(name = "client_skill",
  joinColumns = { @JoinColumn(name = "client_id") },
  inverseJoinColumns = { @JoinColumn(name = "skill_id") })
  public List<Skill> getSkills() {return skills;}
  public void setSkills(List<Skill> skills){this.skills=skills;}   
  @OneToMany(mappedBy="client", targetEntity=Address.class,   
  fetch=FetchType.EAGER, cascade = CascadeType.ALL)
  public List<Address> getAddresses() {return addresses;}
  public void setAddresses(List<Address> addresses) {
  this.addresses = addresses;
  }
  @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
  @JoinColumn(name="detail_id")
  public ClientDetail getClientDetail(){return clientDetail;}
  public void setClientDetail(ClientDetail clientDetail) {
  this.clientDetail = clientDetail;
  }
  public String getPassword() {return password;}
  public void setPassword(String password){this.password = password;}
  public String getUsername() {return username;}
  public void setUsername(String username) {
  this.username = username;
  }
  public Boolean getVerified() {return verified;}
  public void setVerified(Boolean verified){this.verified=verified;}
  }


http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=20advancedentitymapping

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.