• 多对多 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();
    
        }
    
    }
    

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

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

  • 相关阅读:
    tomcat窗口修改显示名字
    Windows Server 2012 安装oracle11g
    oracle查看和修改session和最大连接数
    ORA-12518 TNS:监听程序无法分发客户机连接的解决办法
    Oracle数据导入导出imp/exp :未知的命令开头。。。忽略了剩余的行
    用Dev C++编写第一个C语言程序
    PAT 甲级 1045 Favorite Color Stripe (30 分)(思维dp,最长有序子序列)
    PAT 甲级 1043 Is It a Binary Search Tree (25 分)(链表建树前序后序遍历)*不会用链表建树 *看不懂题...
    PAT 甲级 1044 Shopping in Mars (25 分)(滑动窗口,尺取法,也可二分)
    数据库设计的基本步骤
  • 原文地址:https://www.cnblogs.com/mrcharles/p/4731704.html
Copyright © 2020-2023  润新知