zoukankan      html  css  js  c++  java
  • 多对多 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();
    
        }
    
    }
    

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

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

  • 相关阅读:
    SSIS 错误代码 DTS_E_OLEDB_EXCEL_NOT_SUPPORTED 没有可用的 OLE DB 访问接口 SSIS 的 64 位版本中不支持 Excel 连接管理器
    WIN8安装SQL SERVER2008:出现创建userSettings/Microsoft.SqlServer.Configuration.LandingPage.Properties.Settings 的配置节处理程序时出错的解决办法。
    WIN8安装SQL SERVER2008:出现"MsiGetPRoductInfo” 无法检索 Product Code {47439CBBCF57485B9994BE9E02D29193}的包的 ProductVersion;解决办法。
    解读QT信号与槽机制里 QMetaObject::connectSlotsByName(QObject *o)的源码
    初涉Windows Mobile下的QT程序移植和部署
    C++虚函数探索笔记(1)——虚函数的简单示例分析
    一个不应该发生的BUG。。。
    Qt.Visual.Studio.Integration.1.4.3 与 QT 4.5.0 Commercial for VS2008搭档的一个小问题
    C++ 逗号操作符二三事
    C++虚函数探索笔记(3)——延伸思考:虚函数应用的一些其他情形
  • 原文地址:https://www.cnblogs.com/mrcharles/p/4731704.html
Copyright © 2011-2022 走看看