Hibernate4自学入门(四)——单向多对一映射、junit4生命周期

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


一、 单向多对一映射

创建两个model

public class Class {
private long id;
private String name;
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 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;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
public Class getC() {
return c;
}
public void setC(Class c) {
this.c = c;
}


}
其中:student类中的 成员C 指向了类Class 

配置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="com.gw.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="com.gw.model.Class" cascade="save-update"></many-to-one>
</class>

</hibernate-mapping>

<many-to-one>标签是写在属于many这一方的配置文件里 name指定了对应类的成员 column是外键列名 class指向了 one一方的类 cascade=“save-update”自动联级保存 使得在添加外键之前无需save Class的数据,默认是none 


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.gw.model">

<class name="Class" table="t_class">
<id name="id" column="classId">
<generator class="native"></generator>
</id>

<property name="name" column="className"></property>
</class>

</hibernate-mapping>


二、Junit4

package com.gw.service;

import static org.junit.Assert.*;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.gw.model.Class;
import com.gw.model.Student;
import com.gw.util.HibernateUtil;

public class StudentTest2 {

private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

private Session session;
@BeforeClass
public static void setUpBeforeClass() throws Exception {

}

@AfterClass
public static void tearDownAfterClass() throws Exception {

System.out.println("类初始化后调用");
}

@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 test() {
Class c= new Class();
c.setName("学前班sss");


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

Student s2 = new Student();
s2.setName("李四");
s2.setC(c);

session.save(s1);
session.save(s2);

}

}


setUpBeforeClass() :在类初始化之前执行

tearDownAfterClass():类初始化之后执行

setUp():测试方法执行前执行

 tearDown():测试方法执行后执行