多对多 hibernate映射

数据库:

create table EMPLOYEE
(
  EMPID   NUMBER(6) not null,
  EMPNAME VARCHAR2(32)
)

alter table EMPLOYEE
  add primary key (EMPID)
  ;

  create table PROJECT
(
  PROJID   NUMBER(6) not null,
  PROJNAME VARCHAR2(32)
)

alter table PROJECT
  add primary key (PROJID)
  ;
create table PROJ_EMP
(
  PROJID NUMBER(6),
  EMPID  NUMBER(6)
)

alter table PROJ_EMP
  add constraint FK_EMPLOYEE_PROJ_EMP foreign key (EMPID)
  references EMPLOYEE (EMPID);
alter table PROJ_EMP
  add constraint FK_PROJECT_PROJ_EMP foreign key (PROJID)
  references PROJECT (PROJID);

package com.emp.pojo;

import java.util.HashSet;
import java.util.Set;

/**
 * Project entity. @author MyEclipse Persistence Tools
 */

public class Project implements java.io.Serializable {

    // Fields

    private Integer projid;
    private String projname;

    private Set<Employee> employee = new HashSet<Employee>();

    // Constructors

    /** default constructor */
    public Project() {
    }

    /** full constructor */
    public Project(String projname) {
        this.projname = projname;
    }

    // Property accessors

    public Integer getProjid() {
        return this.projid;
    }

    public void setProjid(Integer projid) {
        this.projid = projid;
    }

    public String getProjname() {
        return this.projname;
    }

    public void setProjname(String projname) {
        this.projname = projname;
    }

    public Set<Employee> getEmployee() {
        return employee;
    }

    public void setEmployee(Set<Employee> employee) {
        this.employee = employee;
    }

}

员工

package com.emp.pojo;

import java.util.HashSet;
import java.util.Set;

/**
 * Employee entity. @author MyEclipse Persistence Tools
 */

public class Employee implements java.io.Serializable {

    // Fields

    private Integer empid;
    private String empname;

    private Set<Project> project=new HashSet<Project>();
    // Constructors

    /** default constructor */
    public Employee() {
    }

    /** full constructor */
    public Employee(String empname) {
        this.empname = empname;
    }

    // Property accessors

    public Integer getEmpid() {
        return this.empid;
    }

    public void setEmpid(Integer empid) {
        this.empid = empid;
    }

    public String getEmpname() {
        return this.empname;
    }

    public void setEmpname(String empname) {
        this.empname = empname;
    }

    public Set<Project> getProject() {
        return project;
    }

    public void setProject(Set<Project> project) {
        this.project = project;
    }

}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.emp.pojo.Employee" table="employee" >
        <id name="empid" type="java.lang.Integer">
            <column name="EMPID" />
            <generator class="native" />
        </id>
        <property name="empname" type="java.lang.String">
            <column name="EMPNAME" length="32" />
        </property>
        <set name="project" table="proj_emp" cascade="save-update">
        <key column="empid" />
        <many-to-many class="com.emp.pojo.Project" column="projid" />
        </set>
    </class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->
<hibernate-mapping>
    <class name="com.emp.pojo.Project" table="project" >
        <id name="projid" type="java.lang.Integer">
            <column name="PROJID" />
            <generator class="native" />
        </id>
        <property name="projname" type="java.lang.String">
            <column name="PROJNAME" length="32" />
        </property>
        <set name="employee" table="proj_emp" cascade="save-update">
            <key column="projid" />
            <many-to-many class="com.emp.pojo.Employee" column="empid"/>
        </set>
    </class>
</hibernate-mapping>
package com.emp.pojo;

import static org.junit.Assert.*;

import java.util.HashSet;
import java.util.Set;


import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.BeforeClass;
import org.junit.Test;

import sessionfactory.HibernateSessionFactory;

public class EmployeeTest {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @Test
    public void test() {
        Session session = HibernateSessionFactory.getSession();
        Transaction tx= session.beginTransaction();
        Project proj1 = new Project();
        proj1.setProjname("qq二次开发");
        Project proj2 = new Project();
        proj2.setProjname("华为");
        Project proj3 = new Project();
        proj3.setProjname("可乐");
        Set<Project> xiaozhangset = new HashSet<Project>();
        Employee xiaozhang = new Employee();
        xiaozhang.setEmpname("张晓华");
        Employee xiaowang = new Employee();
        xiaowang.setEmpname("王国光");

        xiaozhangset.add(proj1);
        xiaozhangset.add(proj2);
        xiaozhang.setProject(xiaozhangset);
        xiaowang.setProject(xiaozhangset);
        session.save(xiaozhang);
        session.save(xiaowang);
        tx.commit();

    }

}

测试,程序分别将记录加入三个表

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2015-07-14 16:20  MrCharles在cnblogs  阅读(140)  评论(0编辑  收藏  举报

导航