Hibernate的自关联与多对多

Hibernate的自关联与多对多

自关联

在hibernate中,你只管查询当前表对象即可,
hibernate会自动关联桥表以及关联表查询出关联对象

以树形节点为例
实体类TreeNode类

package com.hsl.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 children = new HashSet();//关键作用的子节点
	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 getChildren() {
		return children;
	}

	public void setChildren(Set 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 + ", children=" + children + "]";
//	}

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

}

对应的TreeNode.hbm.xml的配置




	
		
			
		
		
		
		
		
		
		
		
		
		
		
		
		
			
			
		
	

添加配置核心的hibernate.cfg.xml文件


		
		

TreeNodeDao类

package com.hsl.four.dao;

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

import com.hsl.four.entity.TreeNode;
import com.hsl.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;
	}
}

JUnit测试类

package com.hsl.four.dao;

import org.junit.Test;

import com.hsl.four.entity.TreeNode;

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


	/**比在easyUi里用的递归方便(不过不随便用)
	 * 在vue的后台要用
	 * 只可以加载直系亲属,即父节点与子节点,其他加载不出来
	 */
	@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的自关联与多对多_第1张图片

多对多

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

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

多对多关系注意事项

1 .一定要定义一个主控方

2 . 多对多删除
2.1 主控方直接删除
2.2 被控方先通过主控方解除多对多关系,再删除被控方
2.3 禁用级联删除

3 . 关联关系编辑,不需要直接操作桥接表,hibernate的主控方会自动维护

下面以书籍表和书籍类型表为例

两个实体类我就不展示了

对应的book.hbm.xml配置:




	
		
		
			
		
		
		
		
		
		
		
		
			
			
			
			
		
	

配置category.hbm.xml




	
		
			
		
		
		
		
		
			
			
		
	

添加核心文件hibernate.cfg.xml配置


		
		

BookDao类

package com.hsl.four.dao;

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

import com.hsl.four.entity.Book;
import com.hsl.four.entity.Category;
import com.hsl.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();
	}

}

相应的JUit测试类

package com.hsl.four.dao;

import org.junit.Test;

import com.hsl.four.entity.Book;
import com.hsl.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());
	}
	
	/**
	 * book.hbm.xml	inverse=fasle
	 * category.hbm.xml inverse=true
	 * 这两个配置文件只有一正一反才会成功
	 * 数据添加正常
	 * 书籍表、桥接表各新增一条数据
	 */
	@Test
	public void test1() {
		Book book = new Book();
		book.setBookName("蓝帽会");
		book.setPrice(10f);
		Category category = new Category();
		category.setCategoryId(3);
//		直接将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);
	}

	/**
	 * book.hbm.xml	inverse=true
	 * category.hbm.xml inverse=true
	 * 只增加书籍表数据
	 * 桥接表不加数据
	 * 原因:双方都没有去维护关系
	 */
	@Test
	public void test2() {
		Book book = new Book();
		book.setBookName("美人为馅");
		book.setPrice(12f);
		Category category = new Category();
		category.setCategoryId(3);
		Category c = this.bookDao.getCategory(category);
		
		book.getCategories().add(c);
		this.bookDao.addBook(book);
//		c.getBooks().add(book);
	}
	
	
}

很重要的是要把两个对应的*.hbm.xml文件搞清楚

你可能感兴趣的:(SSH)