Hibernate入门(十一)多对多案例

Hibernate多对多案例

  1.用户对角色

DROP TABLE IF EXISTS emp_role;
DROP TABLE IF EXISTS employee;
DROP TABLE IF EXISTS role;

CREATE TABLE employee(
        eid        INT    PRIMARY KEY AUTO_INCREMENT,
        ename      VARCHAR(20),
        egender   CHAR(10),
        ephone     VARCHAR(30)
);

CREATE TABLE role(
        rtype        INT  PRIMARY KEY AUTO_INCREMENT,
        rname        VARCHAR(20)
);

CREATE TABLE emp_role( 
        r_eid      INT,
        r_rtype    INT,
        
        CONSTRAINT pk_r_eid_type PRIMARY KEY(r_eid,r_rtype),
        CONSTRAINT fk_employee_emp_role FOREIGN KEY(r_eid) REFERENCES employee(eid),
        CONSTRAINT fk_role_emp_role FOREIGN KEY(r_rtype) REFERENCES role(rtype)
);

Employee.hbm.xml

<!-- 配置多对多属性 -->
        <!-- 
            set标签:
                name:关联的另一方的集合的属性名称
                table:中间表的名称
         -->
        <set name="roles" table="emp_role" inverse="true">
            <!-- 
                key标签:
                    column:当前对象在中间表中的外键的名称
             -->
            <key column="r_eid"></key>
            <!-- 
                many-to-many标签:
                    class: 关联的另一方的类的全路径
                        
             -->
            <many-to-many column="r_rtype" class="Role" ></many-to-many>
        </set>

Role.hbm.xml

<!-- 配置多对多属性 -->
<set name="employees" table="emp_role" cascade="save-update"> <key column="r_rtype"></key> <many-to-many column="r_eid" class="Employee" ></many-to-many>
</set>
/*
     * new员工1个,new角色两个,角色给员工
     */
    @Test
    public void fun() {
        Session session = HibernateUtils.getSession();
        session.getTransaction().begin();

        try {
            Employee emp = new Employee();
            emp.setEname("刘欢");
            emp.setEgender("男");
            emp.setEphone("15497863155");

            Role role = new Role();
            role.setRname("前台");

            Role role1 = new Role();
            role1.setRname("保洁");

            role.getEmployees().add(emp);
            role1.getEmployees().add(emp);

            session.save(role);
            session.save(role1);
        } catch (Exception e) {
            session.getTransaction().rollback();
            e.printStackTrace();
        }

        session.getTransaction().commit();
        session.close();
    }

问题:Hibernate生成的SQL:不知道为什么还是会重复维护关系,我明明已经inverse="true"了, 如果我给role配置inverse不给employee配置inverse="true"的话,那根本就都放弃维护了,完全搞不懂,所以我就单纯让employee放弃维护,但是,还是重复维护关系了。而且,按理来说应该是被动方放弃维护关系,也就是说应该role方放弃维护,哎...脑瓜子疼

Hibernate:
    insert
    into
        role
        (rname)
    values
        (?)
Hibernate:
    insert
    into
        employee
        (ename, egender, ephone)
    values
        (?, ?, ?)
Hibernate:
    insert
    into
        role
        (rname)
    values
        (?)
Hibernate:
    insert
    into
        emp_role
        (r_rtype, r_eid)
    values
        (?, ?)
Hibernate:
    insert
    into
        emp_role
        (r_rtype, r_eid)
    values
        (?, ?)

 

 

 role的rtype会是4,5,是因为之前已经增删过几次数据了。

 

2.解除角色(这里就解除这个人的保洁角色把,5)

    /**
     * 将id为1002127的员工的保洁身份去除
     */
    @Test
    public void fun2() {
        Session session = HibernateUtils.getSession();
        session.getTransaction().begin();

        try {
            Employee emp = session.get(Employee.class, 1002127);
            Role role = session.get(Role.class, 5);
            role.getEmployees().remove(emp);
        } catch (Exception e) {
            session.getTransaction().rollback();
            e.printStackTrace();
        }

        session.getTransaction().commit();
        session.close();
    }

 

 

Hibernate生成的SQL:

Hibernate:
    select
        employee0_.eid as eid1_2_0_,
        employee0_.ename as ename2_2_0_,
        employee0_.egender as egender3_2_0_,
        employee0_.ephone as ephone4_2_0_
    from
        employee employee0_
    where
        employee0_.eid=?
Hibernate:
    select
        role0_.rtype as rtype1_4_0_,
        role0_.rname as rname2_4_0_
    from
        role role0_
    where
        role0_.rtype=?
Hibernate:
    select
        employees0_.r_rtype as r_rtype1_1_0_,
        employees0_.r_eid as r_eid2_1_0_,
        employee1_.eid as eid1_2_1_,
        employee1_.ename as ename2_2_1_,
        employee1_.egender as egender3_2_1_,
        employee1_.ephone as ephone4_2_1_
    from
        emp_role employees0_
    inner join
        employee employee1_
            on employees0_.r_eid=employee1_.eid
    where
        employees0_.r_rtype=?
Hibernate:
    delete
    from
        emp_role
    where
        r_rtype=?

3.再给他添加一个角色把,把保洁的角色再次赋予给他

/**
     * 给id为1002127的员工添加保洁的身份
     */
    @Test
    public void fun3() {
        Session session = HibernateUtils.getSession();
        session.getTransaction().begin();

        try {
            Employee emp = session.get(Employee.class, 1002127);
            Role role = session.get(Role.class, 5);
            role.getEmployees().add(emp);
        } catch (Exception e) {
            session.getTransaction().rollback();
            e.printStackTrace();
        }

        session.getTransaction().commit();
        session.close();
    }

 

 

Hibernate生成的SQL

Hibernate:
    select
        employee0_.eid as eid1_2_0_,
        employee0_.ename as ename2_2_0_,
        employee0_.egender as egender3_2_0_,
        employee0_.ephone as ephone4_2_0_
    from
        employee employee0_
    where
        employee0_.eid=?
Hibernate:
    select
        role0_.rtype as rtype1_4_0_,
        role0_.rname as rname2_4_0_
    from
        role role0_
    where
        role0_.rtype=?
Hibernate:
    select
        employees0_.r_rtype as r_rtype1_1_0_,
        employees0_.r_eid as r_eid2_1_0_,
        employee1_.eid as eid1_2_1_,
        employee1_.ename as ename2_2_1_,
        employee1_.egender as egender3_2_1_,
        employee1_.ephone as ephone4_2_1_
    from
        emp_role employees0_
    inner join
        employee employee1_
            on employees0_.r_eid=employee1_.eid
    where
        employees0_.r_rtype=?
Hibernate:
    insert
    into
        emp_role
        (r_rtype, r_eid)
    values
        (?, ?)

posted @ 2018-11-19 19:36  瓶子coder  阅读(180)  评论(0编辑  收藏  举报