Hibernate4学习之一对多关系配置(四)
程序员文章站
2022-04-14 09:21:35
...
一对多关系的实现
1.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="com/newbeedaly/model/Student.hbm.xml"/>
<mapping resource="com/newbeedaly/model/Class.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.Student类和Class类,以及表结构生成配置Student.hbm.xml和Class.hbm.xml
package com.newbeedaly.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 + "]";
}
}
package com.newbeedaly.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;
}
}
<?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.newbeedaly.model">
<class name="Student" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id>
<property name="name" column="stuName"></property>
<!--
班级学生一对多关系的配置:级联关系
cascade="save-update" 自动保存关联表,如Test2。默认值为none,不保存。
-->
<many-to-one name="c" column="classId" class="com.newbeedaly.model.Class" cascade="save-update"></many-to-one>
</class>
</hibernate-mapping>
<?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.newbeedaly.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>
3.测试代码StudentTest 和StudentTest 2
package com.newbeedaly.service;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.newbeedaly.model.Class;
import com.newbeedaly.model.Student;
import com.newbeedaly.util.HibernateUtil;
public class StudentTest {
public static void main(String[] args) {
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
Class c=new Class();
c.setName("08计本");
session.save(c);
Student s1=new Student();
s1.setName("张三");
s1.setC(c);
Student s2=new Student();
s2.setName("李四");
s2.setC(c);
session.save(s1);
session.save(s2);
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
}
}
package com.newbeedaly.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.newbeedaly.model.Class;
import com.newbeedaly.model.Student;
import com.newbeedaly.util.HibernateUtil;
public class StudentTest2 {
private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
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("08计本");
session.save(c);
Student s1=new Student();
s1.setName("张三");
s1.setC(c);
Student s2=new Student();
s2.setName("李四");
s2.setC(c);
session.save(s1);
session.save(s2);
}
@Test
public void testSaveClassAndStudentWithCascade() {
Class c=new Class();
c.setName("08计本");
Student s1=new Student();
s1.setName("张三");
s1.setC(c);
Student s2=new Student();
s2.setName("李四");
s2.setC(c);
session.save(s1);
session.save(s2);
}
}