简体   繁体   中英

Hibernate bean mappings for joining 3 tables

I have never written the Hibernate code by joining 3 tables and I am facing difficulties.

I have got the following 3 database tables.

CREATE TABLE EMPLOYEE (  
  EMP_ID int(6) NOT NULL AUTO_INCREMENT,  
  EMP_NAME varchar(20) NOT NULL,  
  PRIMARY KEY (EMP_ID));     

CREATE TABLE SKILLS (  
  SKILL_NAME varchar(20) NOT NULL,  
  PRIMARY KEY (SKILL_NAME)
);  

CREATE TABLE EMPLOYEE_SKILLS( 
  EMPLOYEE_ID int(6) NOT NULL,  
  SKILL varchar(20) NOT NULL,   
  KEY EMPLOYEE_fk (EMPLOYEE_ID),  
  KEY SKILLS_fk (SKILL),  
  CONSTRAINT EMPLOYEE_fk FOREIGN KEY (`EMPLOYEE_ID`) REFERENCES `EMPLOYEE` (`EMP_ID`) ON DELETE CASCADE ON UPDATE CASCADE,  
  CONSTRAINT SKILLS_fk FOREIGN KEY (`SKILL`) REFERENCES `SKILLS` (`SKILL_NAME`) ON DELETE CASCADE ON UPDATE CASCADE, 
  UNIQUE(EMPLOYEE_ID,SKILL)
);

Please find the bean classes as below:

@Entity
@Table(name = "EMPLOYEE")
public class EmployeeBean implements Serializable {

    @Id
    @Column(name="EMP_ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int employeeid;

    @Column(name="EMP_NAME")
    private String employeeName;

    //Add Mappings here
}


@Entity
@Table(name="SKILLS")
public class SkillsBean implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="SKILL_NAME", unique = true)
    private String skillName;

    //Add your mappings here
}

@Entity
@Table(name = "EMPLOYEE_SKILLS")
public class EmployeeSkillsBean implements Serializable {

    @Embeddable
    public static class Id implements Serializable {

        @Column(name="EMPLOYEE_ID")
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private int employeeId;

        @Column(name="SKILL")
        private String skillName;

        public Id() {
        }

        public Id(int employeeId, String skillName) {
            this.employeeId = employeeId;
            this.skillName = skillName;
        }

        public boolean equals(Object obj) {
            if(obj != null && obj instanceof Id) {
                Id idObj = (Id)obj;
                return this.employeeId == idObj.employeeId && this.skillName.equals(idObj.skillName) ;
            } else {
                return false;
            }
        }

        public int hashcode() {
            return employeeId+"".hashCode() + skillName.hashCode();
        }
    }

    @EmbeddedId
    private Id id = new Id();

    //Add your mappings here    
}

My requirement is that from UI(JSP), Users enter Employee Name and Skill Name and it should be inserted into EMPLOYEE_SKILLS table. This tables hold multiple employees and their multiple skills.

Could you please help by adding the required Hibernate mappings to the above Beans ? Also can you help on how will be the DAO insert() method to save the employee name and skill to EMPLOYEE_SKILLS table ?

@Entity
@Table(name = "EMPLOYEE")
public class EmployeeBean implements Serializable {

    @Id
    @Column(name="EMP_ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int employeeid;

    @Column(name="EMP_NAME")
    private String employeeName;

    @OneToMany(mappedBy="employee")
    private List<SkillsBean> skills;

    //Add Mappings here
}


@Entity
@Table(name="SKILLS")
public class SkillsBean implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="SKILL_NAME", unique = true)
    private String skillName;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="EMP_ID")
    private EmployeeBean employee;

    //Add your mappings here
}

I assume one employee can have many skills

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM