Hibernate-关联关系映射

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/qq_31776219/article/details/51580551

Employee.java

package hibch05.entity;

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

public class Employee {
    private Integer empid;

    public Integer getEmpid() {
        return empid;
    }

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

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public Set<Project> getProjects() {
        return projects;
    }

    public void setProjects(Set<Project> projects) {
        this.projects = projects;
    }

    private String ename;
    private Set<Project> projects = new HashSet<Project>();
}

Employee.hbm.xml

<?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" >
<hibernate-mapping>
    <class name="hibch05.entity.Employee" table="employee"
        schema="scott">
        <id name="empid" type="java.lang.Integer" column="empid">
            <generator class="sequence">
                <param name="sequence">seq_id</param>
            </generator>
        </id>
        <property name="ename" column="ename" />
        <set name="projects" table="r_pro_emp" inverse="true">
           <key column="employeeId"/>
           <many-to-many class="hibch05.entity.Project" column="projectId"/>
        </set>
    </class>
</hibernate-mapping>

project.java

package hibch05.entity;

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

public class Project {
    private Integer proid;
    private String proname;
    private Set<Employee> employees = new HashSet<Employee>();

    public Integer getProid() {
        return proid;
    }

    public void setProid(Integer proid) {
        this.proid = proid;
    }

    public String getProname() {
        return proname;
    }

    public void setProname(String proname) {
        this.proname = proname;
    }

    public Set<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(Set<Employee> employees) {
        this.employees = employees;
    }
}

Project.hbm.xml

<?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" >
<hibernate-mapping>
    <class name="hibch05.entity.Project" table="project" schema="scott">
        <id name="proid" type="java.lang.Integer">
            <column name="proid" />
            <generator class="sequence">
                <param name="sequence">seq_id</param>
            </generator>
        </id>
        <property name="proname" column="proname"/>
        <set name="employees" table="r_pro_emp" cascade="save-update">
          <key column="projectId"/>
          <many-to-many class="hibch05.entity.Employee" column="employeeId"/>
        </set>



    </class>
</hibernate-mapping>

Test.java

package junit.test;

import static org.junit.Assert.*;
import hibch05.dao.HibernateSessionFactory;
import hibch05.entity.Employee;
import hibch05.entity.Project;

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

public class Ch05Test {

    @Test
    public void test() {
        Employee emp=new Employee();
        emp.setEname("张三");
        Employee emp2=new Employee();
        emp2.setEname("李四");
        Employee emp3=new Employee();
        emp3.setEname("王五");

        Project prj=new Project();
        prj.setProname("南水北调");
        Project prj2=new Project();
        prj2.setProname("西气东输");
        Project prj3=new Project();
        prj3.setProname("三峡工程");
        //设置员工所参与的项目
        emp.getProjects().add(prj);
        emp.getProjects().add(prj3);

        emp2.getProjects().add(prj2);

        emp3.getProjects().add(prj2);
        emp3.getProjects().add(prj3);
        //设置项目有哪些员工参加
        prj.getEmployees().add(emp);

        prj2.getEmployees().add(emp2);
        prj2.getEmployees().add(emp3);

        prj3.getEmployees().add(emp);
        prj3.getEmployees().add(emp3);

        Session session=HibernateSessionFactory.getSession();
        session.save(prj);
        session.save(prj2);
        session.save(prj3);
        session.beginTransaction().commit();//提交事务

    }

    public void test2(){
        Session session=HibernateSessionFactory.getSession();

        Project pro=new Project();
        pro.setProname("三峡工程");

        Project pro2=new Project();
        pro2.setProname("南水北调工程");

        Employee emp=new Employee();
        emp.setEname("小胖");

        Employee emp2=new Employee();
        emp2.setEname("大胖");

        Employee emp3=new Employee();
        emp3.setEname("二胖");

        //添加工程项目到员工的信息中
        emp.getProjects().add(pro);
        emp2.getProjects().add(pro);
        emp2.getProjects().add(pro2);
        emp3.getProjects().add(pro2);

        //添加员工信息到工程项目中
        pro.getEmployees().add(emp);
        pro.getEmployees().add(emp2);
        pro2.getEmployees().add(emp2);
        pro2.getEmployees().add(emp3);

        Transaction tx=session.beginTransaction();
        session.save(pro);
        session.save(pro2);
        tx.commit();
    }

}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页