一对一映射

来源:互联网 发布:2016外汇储备数据 编辑:程序博客网 时间:2024/04/28 08:35

<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>

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝不吃奶吃手怎么办 三个月的宝宝光吃手不吃奶怎么办 九个月宝宝积热怎么办 前门牙露神经了怎么办 7岁宝宝吐舌头怎么办 连续液精几个月了怎么办 接吻时对方伸舌头我要怎么办 亲嘴不想让对方伸舌头怎么办 喝醉酒想吐吐不出来怎么办 凉了胃怎么办喝什么药 生宝宝后屁股扁了怎么办 屁股又宽又扁怎么办 烫伤一年多色差没有恢复怎么办 上古卷轴5不能动怎么办 上古卷轴ol卡死怎么办 小妹妹这么骚长大了怎么办 同事借钱忘了还怎么办 怀了二胎想离婚怎么办 如果闺蜜疏远了怎么办 考上博士但是硕士要延期怎么办 硕士延期也不能毕业怎么办 硕士论文工作量太少被延期了怎么办 竞彩足球比赛延期中断怎么办? 竞彩足球输了怎么办 讯飞语音不兼容百度怎么办 虎牙直播不兼容语音怎么办 为什么手机打开游戏就死机怎么办 2007cad打开时时死机怎么办 手机qq总是无响应怎么办 英雄联盟登录服务器未响应怎么办 苹果7p照相死机怎么办 小米手机qq打不开怎么办啊 电脑qq老是闪退怎么办 微信摄像卡住了怎么办 电脑打开应用程序没反应怎么办 宇飞来手机锁了怎么办 宇飞来手机忘记密码怎么办 vivo手机突然开不了机怎么办 苹果六关不了机怎么办 微信老是无响应怎么办 打游戏被骂了怎么办