Hibernate 多对一关联关系的例子

图示:
Hibernate 多对一关联关系的例子_第1张图片
 

 

创建 Teacher 实体类和它的映射文件Teacher.hbm.xml

package entity;

public class Teacher {
	private int id; // 编号
	private String name;// 姓名

	/*
	 * 构造方法
	 */
	public Teacher() {
	}

	public Teacher(String name) {
		this.name = name;
	}

	/*
	 * 封装方法
	 */
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="entity.Teacher" >
		<id name="id" type="java.lang.Integer">
			<generator class="sequence">
				<param name="sequence">seq_teacher</param>
			</generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name"></column>
		</property>
	</class>
</hibernate-mapping>

 创建 Student 实体类和它的映射文件Student.hbm.xml

package entity;

public class Student {
	private int id; // 编号
	private String name;// 姓名
	private Teacher teacher;// 用来作约束的

	/*
	 * 构造方法
	 */
	public Student() {
	}

	public Student(String name) {
		this.name = name;
	}

	/*
	 * 封装方法
	 */
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

}

 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="entity.Student" >
		<id name="id" type="java.lang.Integer">
			<generator class="sequence">
				<param name="sequence">seq_student</param>
			</generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name"></column>
		</property>
		<many-to-one name="teacher" column="tid" class="entity.Teacher"></many-to-one>
	</class>
</hibernate-mapping>

 

配置 hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
	<!-- 数据库URL -->
	<property name="connection.url">
		jdbc:oracle:thin:@localhost:1521:oracle11
	</property>
	<!-- 数据库用户 -->
	<property name="connection.username">A_hr</property>
	<!-- 数据库用户密码 -->
	<property name="connection.password">123456</property>
	<!-- 数据库 JDBC 驱动 -->
	<property name="connection.driver_class">
		oracle.jdbc.driver.OracleDriver
	</property>
	<!-- 是否将运行期生成的 SQL 输出到日志以供调试 -->
	<property name="show_sql">true</property>
	<!-- 每个数据库都有其对应的 Dialect 以匹配其平台特征 -->
	<property name="dialect">
		org.hibernate.dialect.Oracle10gDialect
	</property>
	<property name="hbm2ddl.auto">create</property>
	<mapping resource="entity/Student.hbm.xml" />
	<mapping resource="entity/Teacher.hbm.xml" />
</session-factory>
</hibernate-configuration>

 

package test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import entity.Student;
import entity.Teacher;

public class MaryToOneTest {
	public static void main(String[] args) {
		maryToOne();
	}

	private static void maryToOne() {
		// 构造配置对象,并且读取配置
		Configuration cfg = new Configuration().configure();
		// SessionFactory = 管理连接工厂
		SessionFactory sf = cfg.buildSessionFactory();
		// Session连接
		Session session = sf.openSession();
		// 增删改用事务
		Transaction tx = null;
		
		Teacher teacher =new Teacher("李老师");
		Student student1 = new Student("A学生");
		Student student2 = new Student("B学生");
		student1.setTeacher(teacher);//对象模型,建立两个对象的关联
		student2.setTeacher(teacher);//对象模型,建立两个对象的关联
		
		try {
			tx=session.beginTransaction();
			session.save(teacher);
			session.save(student1);
			session.save(student2);
			tx.commit();
			System.out.println("保存成功!!!");
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}finally{
			session.close();
			sf.close();
		}
		
	}
}

 

控制台的效果图:
Hibernate 多对一关联关系的例子_第2张图片

 

数据库里面的效果图:
Hibernate 多对一关联关系的例子_第3张图片
Hibernate 多对一关联关系的例子_第4张图片
 

 

你可能感兴趣的:(Hibernate)