1. 程式人生 > >hibernate 一對多自關聯查詢(如選單表)

hibernate 一對多自關聯查詢(如選單表)

hibernate 有自帶的選單查詢功能,當查詢的節點有幾千條時,hibernate自帶的選單查詢會造成卡頓的情況,但一般情況下hibernate自帶的選單查詢減少程式設計師的程式碼量,
簡化了程式碼

相關測試資料(MySQL)

-- 一對多雙向自關聯
-- 選單表
-- t_hibernate_sys_tree_node
-- t:表
-- sys:模組名縮寫(system)
-- tree_noe:表名
create table t_hibernate_sys_tree_node
(
  tree_node_id int primary key auto_increment,                                                 -- ID
  tree_node_name varchar(50) not null,                                                         -- 名字
  tree_node_type int not null check(tree_node_type = 1 or tree_node_type = 2),                 -- 節點型別:1 枝節點 2 葉節點

  position int,                                                                             -- 位置
  parent_node_id int,                                                                         -- 父節點ID
  url varchar(1024),                                                                           -- URL
  foreign key(parent_node_id) references t_hibernate_sys_tree_node(tree_node_id)
);


-- drop table t_hibernate_sys_tree_node
-- select * from t_hibernate_sys_tree_node


select * from t_hibernate_sys_tree_node;
truncate table t_hibernate_sys_tree_node;


insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(1,'系統管理',1, 1,null,null);
insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(2,'市場管理',1, 2,null,null);

insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(3,'字典管理',2, 3,1,null);
insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(4,'使用者管理',2, 4,1,null);
insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(5,'角色管理',2, 5,1,null);
insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(6,'許可權管理',1, 6,1,null);

insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(7,'進貨管理',2, 7,2,null);
insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(8,'銷售管理',2, 8,2,null);
insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(9,'庫存管理',2, 9,2,null);

insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(10,'使用者分配角色',2, 10,6,null);
insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(11,'角色授予使用者',2, 11,6,null);
);

配置選單表的對映檔案

<?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.zking.entity.TreeNode" table="t_hibernate_sys_tree_node">
		<id name="treeNode_id" type="java.lang.Integer" column="tree_node_id">
			<generator class="increment" />
		</id>
		<property name="treeNode_name" type="java.lang.String" column="tree_node_name"></property>
		<property name="treeNode_type" 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="treeNode" class="com.zking.entity.TreeNode" column="parent_node_id"></many-to-one>
		<!-- <set name="treeNodes" cascade="save-update" inverse="true" >
			<key column="parent_node_id"></key>
			<one-to-many class="com.zking.entity.TreeNode"/>
		</set> -->
		<!-- 
			list 集合配置也有list標籤,小編比較喜歡用當前這個
			order-by:填的表的列段,按照所填的列段排列
		 -->
		<bag order-by="position" name="treeNodes" cascade="save-update" inverse="true" >
			<key column="parent_node_id"></key>
			<one-to-many class="com.zking.entity.TreeNode"/>
		</bag>
	</class>
</hibernate-mapping>

選單實體類

package com.zking.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class TreeNode implements Serializable {
	/**
	 * 2018年10月25日下午6:30:23
	 */
	private static final long serialVersionUID = -5329689980623631639L;
	private Integer treeNode_id;
	private String treeNode_name;
	private Integer treeNode_type;
	private Integer position;
	private String url;
	//父節點
	private TreeNode treeNode;
	//載入許可權是無序的
//	private Set<TreeNode> treeNodes = new HashSet<>();
	//載入許可權是有序的
	//子節點
	private List<TreeNode> treeNodes = new ArrayList<>();
	private Integer initTreeNode;

	@Override
	public String toString() {
		return "TreeNode [treeNode_id="+ treeNode_id +",treeNode_name="+ treeNode_name +",treeNode_type="+ treeNode_type +",position="+ position +",url="+ url +",treeNode="+ treeNode +",treeNodes="+ treeNodes +",initTreeNode="+ initTreeNode +"]";
	}

	public Integer getTreeNode_id() {
		return treeNode_id;
	}

	public void setTreeNode_id(Integer treeNode_id) {
		this.treeNode_id = treeNode_id;

	}

	public String getTreeNode_name() {
		return treeNode_name;
	}

	public void setTreeNode_name(String treeNode_name) {
		this.treeNode_name = treeNode_name;

	}

	public Integer getTreeNode_type() {
		return treeNode_type;
	}

	public void setTreeNode_type(Integer treeNode_type) {
		this.treeNode_type = treeNode_type;

	}

	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 getTreeNode() {
		return treeNode;
	}

	public void setTreeNode(TreeNode treeNode) {
		this.treeNode = treeNode;

	}

//	public Set<TreeNode> getTreeNodes() {
//		return treeNodes;
//	}
//
//	public void setTreeNodes(Set<TreeNode> treeNodes) {
//		this.treeNodes = treeNodes;
//
//	}

	public Integer getInitTreeNode() {
		return initTreeNode;
	}

	public List<TreeNode> getTreeNodes() {
		return treeNodes;
	}

	public void setTreeNodes(List<TreeNode> treeNodes) {
		this.treeNodes = treeNodes;
	}

	public void setInitTreeNode(Integer initTreeNode) {
		this.initTreeNode = initTreeNode;

	}

	public TreeNode() {
		super();
	}

	public TreeNode(Integer treeNode_id, String treeNode_name, Integer treeNode_type, Integer position, String url,
			TreeNode treeNode, List<TreeNode> treeNodes, Integer initTreeNode) {
		super();
		this.treeNode_id = treeNode_id;
		this.treeNode_name = treeNode_name;
		this.treeNode_type = treeNode_type;
		this.position = position;
		this.url = url;
		this.treeNode = treeNode;
		this.treeNodes = treeNodes;
		this.initTreeNode = initTreeNode;
	}

	
}

選單表許可權查詢

package com.zking.dao.daoImpl;

import java.util.List;

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

import com.zking.entity.TreeNode;
import com.zking.util.HibernateUtli;

public class TreeNodeDaoImpl implements ITreeNodeDaoImpl {
	@Override
	public TreeNode getTreeNode(TreeNode treeNode) {
		Session session = HibernateUtli.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		TreeNode t = null;
		try {
			t = session.get(TreeNode.class, treeNode.getTreeNode_id());
			if (null != t && new Integer(1).equals(treeNode.getInitTreeNode())) {
				Hibernate.initialize(t.getTreeNodes());
			}
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
			throw new RuntimeException("查詢出現異常");
		}
		HibernateUtli.closeSession();
		return t;
	}

測試

package com.zking.test;

import org.junit.Before;
import org.junit.Test;

import com.zking.dao.daoImpl.TreeNodeDaoImpl;
import com.zking.entity.TreeNode;

public class TreeNodeDaoTest {
	private TreeNodeDaoImpl daoImpl = null;
	private TreeNode node = null;
	
	@Before
	public void setUp() throws Exception {
		daoImpl = new TreeNodeDaoImpl();
		node = new TreeNode();
	}
	@Test
	public void testget() {
		this.node.setTreeNode_id(1);
		this.node.setInitTreeNode(1);
		TreeNode treeNode = this.daoImpl.getTreeNode(this.node);
		System.out.println(treeNode.getTreeNode_name());
		for (TreeNode s : treeNode.getTreeNodes()) {
			System.out.println(s.getTreeNode_id() + "," + s.getTreeNode_name());
		}
		/*
		 * 	第二種許可權載入方案
		 *	類似於hibernate的懶載入
		 *	第一載入一級節點和二級節點
		 *	當點選二級節點再去向獲取後臺用於展示
		 */
		
	}

}