<hibernate-mapping package="com.longtop.entity">
<class name="TbDossier" table="TB_DOSSIER">
<id name="sn" type="java.lang.Long" column="SN">
//foreign方式生成同一个主键(foreign在那边就在哪边进行保存,另一方也就自动保存了....)
<generator class="foreign">
<param name="property">tbStudent</param>
</generator>
</id>
<property name="highSchool" type="java.lang.String">
<column name="HIGH_SCHOOL" length="30" not-null="true" />
</property>
<property name="seniorSchool" type="java.lang.String">
<column name="SENIOR_SCHOOL" length="20" />
</property>
<property name="university" type="java.lang.String">
<column name="UNIVERSITY" length="20" />
</property>
//重要
<one-to-one name="tbStudent" class="TbStudent"
cascade="all">
</one-to-one>
</class>
</hibernate-mapping>
tb_dossier的主键sn和对应的tb_student的主键sn是相同值,主要通过foreign的方式生成的,其中如果删除了可以级联操作cascade,即删除了tb_dossier,就会自动删除tb_student。
public class OneToOne {
public static void main(String[] args) throws HibernateException {
SessionFactory sessions = new Configuration().configure().buildSessionFactory();
Session session = sessions.openSession();
Transaction tx = session.beginTransaction();
Long start = new Date().getTime();
System.out.println("starttime:"+start);
//添加、修改、删除方法
OneToOne.createStudent(session,
OneToOne.newStudent(session, "leochou", "26"),
"1中",
"1中",
"厦门大学");
OneToOne.updateDossier(session, new Long("11"));
// OneToOne.updateStudent(session, new Long("31"));
//
// OneToOne.deleteDossier(session, new Long("6"));
// session.save(OneToOne.createStudent(session, "lucy", "20"));
tx.commit();
session.close();
Long end = new Date().getTime();
System.out.println("endtime:"+end);
System.out.println("总共用了"+(end-start)+"的时间");
}
private static TbStudent newStudent(Session session,String name,String age) throws HibernateException {
TbStudent student = new TbStudent();
student.setName(name);
student.setAge(age);
return student;
}
private static void createStudent(Session session,
TbStudent student,
String highSchool,
String seniorSchool,
String university) throws HibernateException {
TbDossier dossier = new TbDossier();
dossier.setHighSchool(highSchool);
dossier.setSeniorSchool(seniorSchool);
dossier.setUniversity(university);
dossier.setTbStudent(student);
session.save(dossier);
}
private static void updateDossier(Session session,Long sn) throws HibernateException {
TbDossier dossier = (TbDossier) session.load(TbDossier.class, sn);
dossier.setSeniorSchool("八中");
dossier.getTbStudent().setName("change dossier");
session.update(dossier);
}
private static void updateStudent(Session session,Long sn) throws HibernateException {
TbStudent student = (TbStudent) session.load(TbStudent.class, sn);
student.setName("change student");
session.update(student);
}
private static void deleteDossier(Session session,Long sn) throws HibernateException {
TbDossier dossier = (TbDossier) session.load(TbDossier.class, sn);
session.delete(dossier);
}
}
注意:
为了保证每个同学都有一个档案,所以创建学生的时候,必须session.save(tbDossier)。这样才能tb_student与tb_dossier同时创建。
(二)tb_dossier通过student_sn外键关联tb_student(这个就是多对一了..,在dossier表中多建立一个字段叫student_sn)。
两种实现方式的最大不同在于:1、tb_dossier必须新增一个字段student_sn存放对应的tb_student的sn,2、配置文件的不同,tb_dossier主键的生成不再以来tb_student的主键,独立生成。两个的关系用<many to one>表示,注意 unique="true"必须设为true。
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.longtop.entity">
<class name="TbDossier" table="TB_DOSSIER">
<id name="sn" type="java.lang.Long" column="SN">
<generator class="sequence">
<param name="sequence">seq_tb_dossier_sn</param>
</generator>
</id>
<property name="highSchool" type="java.lang.String">
<column name="HIGH_SCHOOL" length="30" not-null="true" />
</property>
<property name="seniorSchool" type="java.lang.String">
<column name="SENIOR_SCHOOL" length="20" />
</property>
<property name="university" type="java.lang.String">
<column name="UNIVERSITY" length="20" />
</property>
<many-to-one name="tbStudent"
class="com.longtop.entity.TbStudent"
column="STUDENT_SN"
unique="true" cascade="all">
</many-to-one>
</class>
</hibernate-mapping>