- 什么是关联(association)
1.1 关联指的是类之间的引用关系。如果类A与类B关联,那么被引用的类B将被定义为类A的属性。例如:
public class A{
private B b = new B;
public A(){}
}
1.2 关联的分类:关联可以分为一对一、一对多/多对一、多对多关联
关联是有方向的
如下一对多:
package com.zking.five.entity;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class TreeNode {
private Integer treenodeId;
private String treenodeName;
private Integer treenodeType;
private Integer position;
private TreeNode parent;
private String url;
// private Set<TreeNode> children = new HashSet<TreeNode>();
private List<TreeNode> children = new ArrayList<TreeNode>();
private Integer initChildren = 0;
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 List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
public void setInitChildren(Integer initChildren) {
this.initChildren = initChildren;
}
public Integer getTreenodeId() {
return treenodeId;
}
public void setTreenodeId(Integer treenodeId) {
this.treenodeId = treenodeId;
}
public String getTreenodeName() {
return treenodeName;
}
public void setTreenodeName(String treenodeName) {
this.treenodeName = treenodeName;
}
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;
}
}
配置文件如下:
<?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 table="t_hibernate_sys_tree_node" name="com.zking.five.entity.TreeNode">
<id name="treenodeId" type="java.lang.Integer" column="tree_node_id">
<!-- class指的表的主键对应生成的类,大白话就是id自增长 -->
<generator class="increment"></generator>
</id>
<property name="treenodeName" 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.zking.five.entity.TreeNode" column="parent_node_id"></many-to-one>
<!-- <set name="children" cascade="save-update" inverse="true">
<key column="parent_node_id"></key>
<one-to-many class="com.zking.five.entity.TreeNode"/>
</set> -->
<bag name="children" cascade="save-update" inverse="true">
<key column="parent_node_id"></key>
<one-to-many class="com.zking.five.entity.TreeNode"/>
</bag>
</class>
</hibernate-mapping>