hibernate关联关系(多对多)

1. 数据库的多对多
1.1 数据库中不能直接映射多对多
处理:创建一个桥接表(中间表),将一个多对多关系转换成两个一对多

注1:数据库多表联接查询
永远就是二个表的联接查询

A B C D
t1 C
t2 D
t3

注2:交叉连接
注3:外连接:left(左)/right(右)/full(左右)
主从表:连接条件不成立时,主表记录永远保留,与null匹配

A B AB
select * from A,B,AB WHERE A.aID=AB.aID and b.bid = AB.bid
where
在hibernate中,你只管查询当前表对象即可,
hibernate会自动关联桥表以及关联表查询出关联对象

Book Category Book_category
select * from Book b,Book_category bc,category where b.bid = bc.bid and bc.cid = c.cid
and bid = 2

2. hibernate的多对多
2.1 hibernate可以直接映射多对多关联关系(看作两个一对多)

3. 多对多关系注意事项
3.1 一定要定义一个主控方
3.2 多对多删除
3.2.1 主控方直接删除
3.2.2 被控方先通过主控方解除多对多关系,再删除被控方
3.2.3 禁用级联删除
3.3 关联关系编辑,不需要直接操作桥接表,hibernate的主控方会自动维护

一对多关联关系案例:

实体类

package com.huang.four.entity;

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

public class TreeNode {
    private Integer nodeId;
    private String nodeName;
    private Integer treeNodeType;
    private Integer position;
    private String url;
    private TreeNode parent;
    private Set<TreeNode> children = new HashSet<TreeNode>();
    private Integer initChildren = 0;

    public Integer getNodeId() {
        return nodeId;
    }

    public void setNodeId(Integer nodeId) {
        this.nodeId = nodeId;
    }

    public String getNodeName() {
        return nodeName;
    }

    public void setNodeName(String nodeName) {
        this.nodeName = nodeName;
    }

    public Integer getTreeNodeType() {
        return treeNodeType;
    }

    public void setTreeNodeType(Integer treeNodeType) {
        this.treeNodeType = treeNodeType;
    }

    public Integer getPosition() {
        return position;
    }

    public void setPosition(Integer position) {
        this.position = position;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public Set<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(Set<TreeNode> children) {
        this.children = children;
    }

    public Integer getInitChildren() {
        return initChildren;
    }

    public void setInitChildren(Integer initChildren) {
        this.initChildren = initChildren;
    }

    @Override
    public String toString() {
        return "TreeNode [nodeId=" + nodeId + ", nodeName=" + nodeName + ", treeNodeType=" + treeNodeType
                + ", position=" + position + ", url=" + url + ", initChildren=" + initChildren + "]";
    }

}

配置实体映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.huang.four.entity.TreeNode" table="t_hibernate_sys_tree_node">
        <id name="nodeId" type="java.lang.Integer" column="tree_node_id">
            <generator class="increment" />
        </id>
        <property name="nodeName" type="java.lang.String"
            column="tree_node_name">
        </property>
        <property name="treeNodeType" type="java.lang.Integer"
            column="tree_node_type">
        </property>
        <property name="position" type="java.lang.Integer"
            column="position">
        </property>
        <property name="url" type="java.lang.String"
            column="url">
        </property>

        <many-to-one name="parent" class="com.huang.four.entity.TreeNode" column="parent_node_id"/>

        <set name="children" cascade="save-update" inverse="true">
            <key column="parent_node_id"></key>
            <one-to-many class="com.huang.four.entity.TreeNode"/>
        </set>
    </class>
</hibernate-mapping>

然后用junit测试

TreeNodeDao

package com.huang.four.dao;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.huang.four.entity.TreeNode;
import com.huang.two.util.SessionFactoryUtils;

public class TreeNodeDao {
    public TreeNode load(TreeNode treeNode) {
        Session session = SessionFactoryUtils.openSession();
        Transaction transaction = session.beginTransaction();
        TreeNode t = session.load(TreeNode.class, treeNode.getNodeId());
        if(t != null && new Integer(1).equals(treeNode.getInitChildren())) {
            Hibernate.initialize(t.getChildren());
            Hibernate.initialize(t.getParent());
        }
        transaction.commit();
        session.close();
        return t;
    }
}

TreeNodeDaoTest

package com.huang.four.dao;

import org.junit.Test;

import com.huang.four.entity.TreeNode;

public class TreeNodeDaoTest {
    private TreeNodeDao treeNodeDao = new TreeNodeDao();

//    @Before
//    public void setUp() throws Exception {
//    }
//
//    @After
//    public void tearDown() throws Exception {
//    }

    @Test
    public void testLoad() {
        TreeNode treeNode = new TreeNode();
        treeNode.setNodeId(6);
        treeNode.setInitChildren(1);

        TreeNode t = this.treeNodeDao.load(treeNode);
        System.out.println(t);
        System.out.println(t.getParent());
        System.out.println(t.getChildren());
    }

}

运行结果:

Hibernate:
    select
        treenode0_.tree_node_id as tree_nod1_6_0_,
        treenode0_.tree_node_name as tree_nod2_6_0_,
        treenode0_.tree_node_type as tree_nod3_6_0_,
        treenode0_.position as position4_6_0_,
        treenode0_.url as url5_6_0_,
        treenode0_.parent_node_id as parent_n6_6_0_
    from
        t_hibernate_sys_tree_node treenode0_
    where
        treenode0_.tree_node_id=?
Hibernate:
    select
        children0_.parent_node_id as parent_n6_6_0_,
        children0_.tree_node_id as tree_nod1_6_0_,
        children0_.tree_node_id as tree_nod1_6_1_,
        children0_.tree_node_name as tree_nod2_6_1_,
        children0_.tree_node_type as tree_nod3_6_1_,
        children0_.position as position4_6_1_,
        children0_.url as url5_6_1_,
        children0_.parent_node_id as parent_n6_6_1_
    from
        t_hibernate_sys_tree_node children0_
    where
        children0_.parent_node_id=?
Hibernate:
    select
        treenode0_.tree_node_id as tree_nod1_6_0_,
        treenode0_.tree_node_name as tree_nod2_6_0_,
        treenode0_.tree_node_type as tree_nod3_6_0_,
        treenode0_.position as position4_6_0_,
        treenode0_.url as url5_6_0_,
        treenode0_.parent_node_id as parent_n6_6_0_
    from
        t_hibernate_sys_tree_node treenode0_
    where
        treenode0_.tree_node_id=?
TreeNode [nodeId=6, nodeName=权限管理, treeNodeType=1, position=6, url=null, initChildren=0]
TreeNode [nodeId=1, nodeName=系统管理, treeNodeType=1, position=1, url=null, initChildren=0]
[TreeNode [nodeId=10, nodeName=用户分配角色, treeNodeType=2, position=10, url=null, initChildren=0], TreeNode [nodeId=11, nodeName=角色授予用户, treeNodeType=2, position=11, url=null, initChildren=0]]

下面是多对多关联

实体类  

Book

package com.huang.four.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Book implements Serializable{
//    book_id int primary key auto_increment,
//       book_name varchar(50) not null,
//       price float not null
    private Integer bookId;
    private String bookName;
    private Float price;

    private Set<Category> categories = new HashSet<Category>();
    private Integer initCategories = 0;

    public Integer getInitCategories() {
        return initCategories;
    }

    public void setInitCategories(Integer initCategories) {
        this.initCategories = initCategories;
    }

    public Integer getBookId() {
        return bookId;
    }

    public void setBookId(Integer bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public Set<Category> getCategories() {
        return categories;
    }

    public void setCategories(Set<Category> categories) {
        this.categories = categories;
    }

    @Override
    public String toString() {
        return "Book [bookId=" + bookId + ", bookName=" + bookName + ", price=" + price + "]";
    }

    public Book(Integer bookId, String bookName) {
        super();
        this.bookId = bookId;
        this.bookName = bookName;
    }

    public Book() {
        super();
    }

}

实体映射文件  

Book.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.huang.four.entity.Book" table="t_hibernate_book">
        <cache usage="read-only" region="com.zking.five.entity.Book"/>
        <id name="bookId" type="java.lang.Integer" column="book_id">
            <generator class="increment" />
        </id>
        <property name="bookName" type="java.lang.String"
            column="book_name">
        </property>
        <property name="price" type="java.lang.Float"
            column="price">
        </property>

        <!--
            set标签:
                table:对应的是中间表        没有实体类,意味着靠两张主表对应的映射文件联合管理数据
                name:当前映射文件对应的实体类对应的属性
                cascade:级联新增修改,说白了就是当前实体类对应的表删除能否影响到关联表的数据
                inverse:中间表的数据维护的权利交给对方
            key标签
                column:当前表t_hibernate_book的主键在中间表的列段bid
            many-to-many:
                column:代表中间表对应的除去当前表t_hibernate_book的非主键的中间表列段
                class:cid对应的类

         -->
        <set table="t_hibernate_book_category" name="categories" cascade="save-update" inverse="true">
            <!-- one -->
            <key column="bid"></key>
            <!-- many -->
            <many-to-many column="cid" class="com.huang.four.entity.Category"></many-to-many>
        </set>
    </class>
</hibernate-mapping>

注意一定要在hibernate.hbm.xml中加入以下代码:

<!-- 多对多关联 -->
        <mapping resource="com/huang/four/entity/Book.hbm.xml" />

测试:

BookDao

package com.huang.four.dao;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.huang.four.entity.Book;
import com.huang.four.entity.Category;
import com.huang.two.util.SessionFactoryUtils;

public class BookDao{
    public Integer addBook(Book book) {
        Session session = SessionFactoryUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Integer bid = (Integer) session.save(book);
        transaction.commit();
        session.close();
        return bid;
    }

    public Integer addCategory(Category category) {
        Session session = SessionFactoryUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Integer cid = (Integer) session.save(category);
        transaction.commit();
        session.close();
        return cid;
    }

    public Category getCategory(Category category) {
        Session session = SessionFactoryUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Category c = session.get(Category.class, category.getCategoryId());
        transaction.commit();
        session.close();
        return c;
    }

    public Book getBook(Book book) {
        Session session = SessionFactoryUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Book b = session.get(Book.class, book.getBookId());
        if (b != null && new Integer(1).equals(book.getInitCategories())) {
            Hibernate.initialize(b.getCategories());
        }
        transaction.commit();
        session.close();
        return b;
    }

    public void delBook(Book book) {
        Session session = SessionFactoryUtils.openSession();
        Transaction transaction = session.beginTransaction();
        session.delete(book);
        transaction.commit();
        session.close();
    }

    public void delCategory(Category category) {
        Session session = SessionFactoryUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Category c = session.get(Category.class, category.getCategoryId());
        if(c!=null) {
            for (Book b : c.getBooks()) {
//                通过在被控方通过主控方来解除关联关系,最后被控方再做删除
                b.getCategories().remove(c);
            }
        }
        session.delete(c);
        transaction.commit();
        session.close();
    }
}

BookDaoTest

package com.huang.four.dao;

import org.junit.Test;

import com.huang.four.entity.Book;
import com.huang.four.entity.Category;

public class BookDaoTest {
    private BookDao bookDao = new BookDao();

    @Test
    public void testGetBook() {
        Book book = new Book();
        book.setBookId(8);
        book.setInitCategories(1);
        Book b = this.bookDao.getBook(book );
        System.out.println(b.getBookName());
        System.out.println(b.getCategories());
    }
}

结果:

Hibernate:
    select
        book0_.book_id as book_id1_0_0_,
        book0_.book_name as book_nam2_0_0_,
        book0_.price as price3_0_0_
    from
        t_hibernate_book book0_
    where
        book0_.book_id=?
Hibernate:
    select
        categories0_.bid as bid1_1_0_,
        categories0_.cid as cid2_1_0_,
        category1_.category_id as category1_2_1_,
        category1_.category_name as category2_2_1_
    from
        t_hibernate_book_category categories0_
    inner join
        t_hibernate_category category1_
            on categories0_.cid=category1_.category_id
    where
        categories0_.bid=?
圣墟
[Category [categoryId=8, categoryName=言情], Category [categoryId=9, categoryName=言情2]]

book.hbm.xml inverse=fasle
 category.hbm.xml inverse=true
 数据添加正常
 书籍表、桥接表各新增一条数据


package com.huang.four.dao;


import org.junit.Test;


import com.huang.four.entity.Book;
import com.huang.four.entity.Category;


public class BookDaoTest {
private BookDao bookDao = new BookDao();

@Test
    public void test1() {
        Book book = new Book();
        book.setBookName("T226");
        book.setPrice(10f);
        Category category = new Category();
        category.setCategoryId(5);
//        直接将category对象加入到新建的book中是错误的,因为此时的category是临时态的,hibernate是不会管理的
//        book.getCategories().add(category);
        Category c = this.bookDao.getCategory(category);

//        c.getBooks().add(book);
        book.getCategories().add(c);
        this.bookDao.addBook(book);
    }

运行结果:

Hibernate:
    select
        category0_.category_id as category1_2_0_,
        category0_.category_name as category2_2_0_
    from
        t_hibernate_category category0_
    where
        category0_.category_id=?
Hibernate:
    select
        max(book_id)
    from
        t_hibernate_book
Hibernate:
    insert
    into
        t_hibernate_book
        (book_name, price, book_id)
    values
        (?, ?, ?)
Hibernate:
    update
        t_hibernate_category
    set
        category_name=?
    where
        category_id=?

book.hbm.xml inverse=true
 category.hbm.xml inverse=true
 只增加书籍表数据
 桥接表不加数据
 原因:双方都没有去维护关系

package com.huang.four.dao;

import org.junit.Test;

import com.huang.four.entity.Book;
import com.huang.four.entity.Category;

public class BookDaoTest {
    private BookDao bookDao = new BookDao();
@Test
    public void test2() {
        Book book = new Book();
        book.setBookName("T226hyc");
        book.setPrice(10f);
        Category category = new Category();
        category.setCategoryId(5);
        Category c = this.bookDao.getCategory(category);

        book.getCategories().add(c);
        this.bookDao.addBook(book);
//        c.getBooks().add(book);
    }

}

结果:

Hibernate:
    select
        category0_.category_id as category1_2_0_,
        category0_.category_name as category2_2_0_
    from
        t_hibernate_category category0_
    where
        category0_.category_id=?
Hibernate:
    select
        max(book_id)
    from
        t_hibernate_book
Hibernate:
    insert
    into
        t_hibernate_book
        (book_name, price, book_id)
    values
        (?, ?, ?)
Hibernate:
    update
        t_hibernate_category
    set
        category_name=?
    where
        category_id=?

原文地址:https://www.cnblogs.com/bf6rc9qu/p/11318014.html

时间: 2024-08-30 10:42:37

hibernate关联关系(多对多)的相关文章

Hibernate映射多对多双向关联关系(小案例)

多对多双向关联关系(Project(工程)/Emp(员工)为案例): 步骤如下: 1.创建Project类,并需要定义集合类型的Emp属性 public class Project { //编号 private Integer pid; //名称 private String pname; //定义集合类型的Emp属性 private Set<Emp> emps=new HashSet<Emp>(); public Integer getPid() { return pid; }

Hibernate关联关系的CRUD

本文以Group和User(一对多.多对一)双向关联为例,介绍关联关系的CRUD   下面先介绍两个属性 cascade:只影响CRUD中的CUD,即存储(save).更新(update).删除(delete) fetch:只影响CRUD中的R,即读取(get.load)   cascade(级联): 此属性仅仅帮助我们简化编程,不是必选项 如果A和B为单向关联,则在主导方设置cascade属性 如果A和B为双向关联,则在双方都要设置cascade属性 如果两个对象之间有关联关系,比如User和

Hibernate关联关系映射(单向篇)

Hibernate关联关系可分为单向关联和双向关联两大类.单向关联可以分为一对一.一对多.多对一和多对多4种关联方式,而多向关联可以分为一对一.一对多和多对多3种关联方式. Hibernate连接管理类HibernateUtil.java public class HibernateUtil { private static SessionFactory sessionFactory; private static final ThreadLocal<Session> threadLocal

关联映射 ---- Hibernate之多对多关系

叙:上一章详细的记录了关联映射中的"一对多|多对一"关系.级联操作.关系的维护等知识点,本章节轻风学习记录的是级联关系中的"多对多"关系: Hibernate的"多对多"级联关系 1. 介绍 在生活中很多关系都是多对多的,比如一个人在公司是技术部的同时也是营销部的(只是个例子),所以,多对对关系是很普遍的,一个对象有多个角色,而一个角色又可以有多个对象,因此最正确的做法是在对象与角色之间创建一个新的表,用来保存对象和角色的主键,方便调用查看相应的

Hibernate单向多对多

最近做一个OA系统,用到了Hibernate框架,我发现,权限和角色的关系是一种多对多的关系,一个权限可以分配给多个角色,一个角色拥有多个权限. 多对多关系有两种,一种是单向的,一种是多向的.对于这个问题,曾经让我很犯难.单纯在语言上理解,会比较复杂,而从代码上理解,可能就会明白了. 下面模拟为角色授权的过程: 1,Hibernate使用Annotation 2,使用Junit进行测试. 3,使用Mysql作为后台数据库. 4,Hibernate不使用自动建表,也不采用反向工程. 过程 : 1,

【SSH系列】Hibernate映射 -- 多对多关联映射

     映射原理 在数据库学习阶段,我们知道,如果实体和实体之间的关系是多对多,那么我们就抽出来第三张表,第一张表和第二张表的主键作为第三表的联合主键,结合我们的hibernate,多对多关联,无论是单向关联还是双向关联都是通过第三张表,将两个表中的主键放到第三张表中做一个关联,用第三张表来解决可能造成的数据冗余问题.今天这篇博文小编来简单的介绍一下hibernate中的多对多关联映射. 在某些系统中,一个用户可以有多个角色,一个角色也可以有多个用户,so,她们之间的关系就是多对多,多对多关联

Hibernate之多对多篇

Hibernate值多对多篇: 首先Hibernate基于数据库持久层框架,好的OR框架.封装了JDBC对数据库繁琐的操作,完全以面向对象的方式来操作数据库,提供了以及一级,二级缓存. 下面就来谈谈Hibernate的优点与缺点: 优点: 1.对jdbc访问数据库进行了封装,简化了繁琐的操作. 2.映射的灵活性 3.非侵入性,移植性好.(就是说只需将你的映射文件及其配置文件移植到相应另一台计算机上照样可以运行,因为表的它是自己检查创建的,这一点非常好,不像Mybatis那 你要去建一个和他的映射

总结Hibernate的多对多关联的4个特点

总结Hibernate的多对多关联的4个特点: 以学生和课程多对多的例子说明,实体类部分代码: public class Course { private Integer cid; private String cname; private Set<Student> stuSet; //getter setter...... } public class Student { private Integer id; private String name; private Date birthd

Hibernate单向多对一级联删除引发的问题

Hibernate单向多对一在级联删除时,会出现一些问题. 下面模拟我遇到的问题: 这次模拟与之前的一次模拟方法一直,博客:http://blog.csdn.net/openjdk8/article/details/38424403 模拟场景:有一个部门表t_dept,职位表t_position. 需求:当删除部门表时,不管职位表有没数据,照样删除.删除职位表就直接删除. 1,建表: 建表: t_dept::部门表 t_position:职位表 CREATE TABLE t_dept(    d