Junit4方法详解
setUpBeforeClass()类初始化前调用
tearDownAfterClass()类初始化后调用
setUp()在测试方法前调用
tearDown()在测试方法后调用
@Test测试方法
在<many-to-one>这端,cascade默认是“none”,假如我们希望在持久化多的一端的时候,自动级联保存和更新一的一端,我们可以把cascade设置为“save-update”
重点内容:1 一对多映射实现(双向) 2 inverse属性 3 级联删除 4 一对多双向自身关联关系映射
model包:
Class.java:
package model;
import java.util.HashSet;
import java.util.Set;
public class Class {
private long id;
private String name;
private Set<Student>students=new HashSet<Student>();
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Class [id=" + id + ", name=" + name + "]";
}
}
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="model">
<class name="Class" table="t_class">
<id name="id" column="classId">
<generator class="native"></generator>
</id>
<property name="name" column="className"></property>
<set name="students" cascade="delete" inverse="true">
<key column="classId"></key>
<one-to-many class="model.Student"/>
</set>
</class>
</hibernate-mapping>
Node.java:
package model;
import java.util.HashSet;
import java.util.Set;
public class Node {
private long id;
private String name;
private Node parentNode;
private Set<Node> childNodes=new HashSet<Node>();
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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="model">
<class name="Node" table="t_node">
<id name="id" column="nodeId">
<generator class="native"></generator>
</id>
<property name="name" column="nodeName"></property>
<many-to-one name="parentNode" column="parentId" class="model.Node" cascade="save-update"></many-to-one>
<set name="childNodes" inverse="true">
<key column="parentId"></key>
<one-to-many class="model.Node"/>
</set>
</class>
</hibernate-mapping>
Student.java:
package model;
public class Student {
private long id;
private String name;
private Class c;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Class getC() {
return c;
}
public void setC(Class c) {
this.c = c;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", c=" + c + "]";
}
}
Student.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="model">
<class name="Student" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id>
<property name="name" column="stuName"></property>
<many-to-one name="c" column="classId" class="model.Class" cascade="save-update"></many-to-one>
</class>
</hibernate-mapping>
service包:
NodeTest.java:
package service;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import util.HibernateUtil;
import model.Node;
public class NodeTest {
SessionFactory sessionfactory=HibernateUtil.getSessionFactory();//获取Session工厂
private Session session;
@Before
public void setUp() throws Exception {
session=sessionfactory.openSession();//生成一个新的session
session.beginTransaction();//开启事务
}
@After
public void tearDown() throws Exception {
session.getTransaction().commit();//提交事务
session.close();//关闭session
}
@Test
public void testSaveMenu() {
Node node=new Node();
node.setName("根节点");
Node subNode1=new Node();
subNode1.setName("子节点1");
Node subNode2=new Node();
subNode2.setName("子节点2");
subNode1.setParentNode(node);
subNode2.setParentNode(node);
session.save(node);
session.save(subNode1);
session.save(subNode2);
}
}
StudentTest.java:
package service;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import util.HibernateUtil;
import model.Class;
import model.Student;
public class StudentTest {
SessionFactory sessionfactory=HibernateUtil.getSessionFactory();//获取Session工厂
private Session session;
@Before
public void setUp() throws Exception {
session=sessionfactory.openSession();//生成一个新的session
session.beginTransaction();//开启事务
}
@After
public void tearDown() throws Exception {
session.getTransaction().commit();//提交事务
session.close();//关闭session
}
@Test
public void testSaveClassAndStudent() {
Class c=new Class();
c.setName("12网络");
Student s1=new Student();
s1.setName("一号");
Student s2=new Student();
s2.setName("二号");
c.getStudents().add(s1);
c.getStudents().add(s2);
s1.setC(c);
s2.setC(c);
session.save(s1);
session.save(s2);
session.save(c);
}
@Test
public void getStudentByClass(){
Class c=(Class) session.get(Class.class,Long.valueOf(1));
Set<Student> students=c.getStudents();
Iterator it=students.iterator();
while(it.hasNext()){
Student s=(Student) it.next();
System.out.println(s);
}
}
@Test
public void testAdd(){
Class c=new Class();
c.setName("12物联网");
Student s=new Student();
s.setName("三号");
session.save(c);
session.save(s);
}
@Test
public void testInverse(){
Class c=(Class) session.get(Class.class,Long.valueOf(1));
Student s=(Student) session.get(Student.class,Long.valueOf(1));
s.setC(c);
c.getStudents().add(s);
}
@Test
public void testDeleteClassCascade(){
Class c=(Class) session.get(Class.class,Long.valueOf(1));
session.delete(c);
}
}
util包:
HibernateUtil.java:
package util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static final SessionFactory sessionFactory=buildSessionFactory();
private static SessionFactory buildSessionFactory() {
Configuration configuration=new Configuration().configure();//实例化配置文件
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();//实例化服务登记类
return configuration.buildSessionFactory(serviceRegistry);//获取Session工厂
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
配置文件:
hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--数据库连接设置 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- 方言 -->
<property name="dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<!-- 控制台显示SQL -->
<property name="show_sql">true</property>
<!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="model/Class.hbm.xml" />
<mapping resource="model/Node.hbm.xml" />
<mapping resource="model/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
回顾:
虽然现在hibernate已经到了4.3的版本,但是他的兼容性还需要极大地完善,这也许就是开源框架最大的问题的地方,不过在我眼中,开源就是造福人类的一个贡献。
在使用hibernate要着重注意拼写的问题,双引号内不报错,所以一个字母的错误可能会耽误几个小时的时间。