Hibernate 多对多 例子

来源:互联网 发布:mysql null的逻辑 编辑:程序博客网 时间:2024/05/18 22:16
public class Employee {    //员工 表    private Integer empid;    private String empname;    //在员工的实体中植入一个项目的集合  一个员工可以参与N个工程    private Set<Project> projects = new HashSet<Project>();    public Set<Project> getProjects() {        return projects;    }    public void setProjects(Set<Project> projects) {        this.projects = projects;    }    public Integer getEmpid() {        return empid;    }    public void setEmpid(Integer empid) {        this.empid = empid;    }    public String getEmpname() {        return empname;    }    public void setEmpname(String empname) {        this.empname = empname;    }}
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.hello.hql2.three">    <class name="Employee" table="Employee" schema="happyy2165">        <id name="empid" column="empid">           <generator class="native"></generator>        </id>        <property name="empname" />        <!--table指的是中间表-->        <set name="projects" table="PROEMP" inverse="true">            <key column="REMPID"></key>            <many-to-many column="RPROID" class="Project"></many-to-many>        </set>    </class></hibernate-mapping>

public class Project {    //项目 表    private Integer proid;    private String proname;    private Set<Employee> employees=new HashSet<Employee>();    public Set<Employee> getEmployees() {        return employees;    }    public void setEmployees(Set<Employee> employees) {        this.employees = employees;    }    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;    }}
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.hello.hql2.three">    <class name="Project" table="Project" schema="happyy2165">        <id name="proid" column="proid">           <generator class="native"></generator>        </id>        <property name="proname" />        <set name="employees" table="PROEMP" inverse="true" cascade="save-update">            <key column="REPROID"></key>            <many-to-many column="REMPID" class="Employee"></many-to-many>        </set>    </class></hibernate-mapping>
public class text20171006_4 {    Configuration cfg;    Session session;    Transaction tx;    SessionFactory factory;    @Before    public  void before(){        cfg=new Configuration().configure();        factory = cfg.buildSessionFactory();        session = HibmentUtil.getSession();        tx = session.beginTransaction();    }    @Test    // 多对多 单向 测试    public  void  aVoid(){        Employee employee = session.get(Employee.class, 1);        System.out.println(employee.getEmpname());        System.out.println("====");        for (Project pro:employee.getProjects()){            System.out.println(pro.getProname());        }    }    @Test    // 多对多 双向 测试    public void setee(){        Employee em=new Employee();        em.setEmpname("张三2");        Employee em2=new Employee();        em2.setEmpname("李四2");        Project pr=new Project();        pr.setProname("嘻哈项目2");        pr.getEmployees().add(em);        pr.getEmployees().add(em2);        session.save(pr);        tx.commit();    }}


<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <!-- Database connection settings -->        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>        <property name="connection.username">happyy2165</property>        <property name="connection.password">a123456a</property>        <!-- JDBC connection pool (use the built-in) -->        <property name="connection.pool_size">1</property>        <!-- SQL dialect -->        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>        <!-- 使用getCurrentSession 进行配置-->        <!-- Enable Hibernate's automatic session context management -->        <property name="current_session_context_class">thread</property>        <!-- Echo all executed SQL to stdout -->        <property name="show_sql">true</property>        <property name="format_sql">true</property>        <!-- Drop and re-create the database schema on startup -->        <property name="hbm2ddl.auto">update</property>        <mapping resource="cn/hello/entity/Dog.hbm.xml"/>        <mapping resource="cn/hello/hql/Dept.hbm.xml"/>        <mapping resource="cn/hello/entity/Emp.hbm.xml"/>        <mapping resource="cn/hello/hql2/one/Emp.hbm.xml"/>        <mapping resource="cn/hello/hql2/one/Dept.hbm.xml"/>        <mapping resource="cn/hello/hql2/two/Emp.hbm.xml"/>        <mapping resource="cn/hello/hql2/two/Dept.hbm.xml"/>        <mapping resource="cn/hello/hql2/three/Employee.hbm.xml"/>        <mapping resource="cn/hello/hql2/three/Project.hbm.xml"/>    </session-factory></hibernate-configuration>



原创粉丝点击