Hibernate学习笔记(三)

时间:2022-03-24 07:35:47

一:班级学生一对多映射实现(双向)

Class类

public class Class {
private int classId;
private String className;
private Set<Student>students=new HashSet<Student>();
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
public int getClassId() {
return classId;
}
public void setClassId(int classId) {
this.classId = classId;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
Class.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.zhu.model">

<class name="Class" table="t_class">
<id name="classId" column="classId">
<generator class="native"></generator>
</id>
<property name="className"></property>
<set name="students" cascade="save-update" inverse="true">
<key column="classId"></key>
<one-to-many class="com.zhu.model.Student" />
</set>

</class>

</hibernate-mapping>
单元测试方法
@Test
public void test() {
Class c=new Class();
c.setClassName("计科1501B");

Student s1=new Student();
s1.setName("张三");

Student s2=new Student();
s2.setName("李四");
c.getStudents().add(s1);
c.getStudents().add(s2);
session.save(c);
}
通过班级获取学生

@Test
public void getStudentsByClass() {
Class c=(Class)session.get(Class.class,1);
for(Student s:c.getStudents()){
System.out.println(s);
}
}


二:inverse 属性(inverse的作用是在hibernate中是通过inverse的设置来决定是由谁来维护表和表之间的关系的)

@Test
public void testInverse() {
Class c=(Class)session.get(Class.class,1);
Student s=(Student)session.get(Student.class,Long.valueOf(1));
s.setC(c);
c.getStudents().add(s);
}


三:级联删除

@Test
public void testDelete() {
Class c=(Class)session.get(Class.class,1);
session.delete(c);
}
级联删除需要设置 cascade="delete"属性,否则会抛出异常,因为不能删除有外键关联的主键的数据项

四:一对多双向自身关联关系映射

Node类

public class Node {
private int id;
private String nodeName;
private Node parentNode;
private Set<Node>childNodes=new HashSet<Node>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public Node getParentNode() {
return parentNode;
}
public void setParentNode(Node parentNode) {
this.parentNode = parentNode;
}
public Set<Node> getChildNodes() {
return childNodes;
}
public void setChildNodes(Set<Node> childNodes) {
this.childNodes = childNodes;
}
}
Node.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.zhu.model">

<class name="Node" table="t_node">
<id name="id">
<generator class="native"></generator>
</id>
<property name="nodeName"></property>

<set name="childNodes" cascade="save-update" inverse="true" >
<key column="nodeId"></key>
<one-to-many class="com.zhu.model.Node" />
</set>
<many-to-one name="parentNode" column="nodeId" cascade="save-update"></many-to-one>
</class>

</hibernate-mapping>

@Test
public void testSaveMenu() {
Node root=new Node();
root.setNodeName("根节点");
Node node1=new Node();
node1.setNodeName("子节点1");
Node node2=new Node();
node2.setNodeName("子节点2");
node1.setParentNode(root);
node2.setParentNode(root);
session.save(node1);
session.save(node2);
}