<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    hibernate 刪除關聯表

    http://www.itzhai.com/hibernate-one-to-many-association-mapping-configuration-and-the-cascade-delete-problem.html首先舉一個簡單的一對多雙向關聯的配置:

    一的一端:QuestionType類

    package com.exam.entity;
    import java.util.Set;
    public class QuestionType {
        private String typeName;
        private char typeUniqueness;
        private Set quesion;
        public String getTypeName() {
            return typeName;
        }
        public void setTypeName(String typeName) {
            this.typeName = typeName;
        }
        public char getTypeUniqueness() {
            return typeUniqueness;
        }
        public void setTypeUniqueness(char typeUniqueness) {
            this.typeUniqueness = typeUniqueness;
        }
        public Set getQuesion() {
            return quesion;
        }
        public void setQuesion(Set quesion) {
            this.quesion = quesion;
        }
    }

    配置文件:

    <hibernate-mapping package="com.exam.entity">
        <class name="QuestionType" table="exam_question_type">
            <id name="typeName" column="type_name"></id>
            <property name="typeUniqueness"  column="type_uniqueness"/>
            <set name="quesion" inverse="true" cascade="delete">
                <key column="question_type_name"/>
                <one-to-many class="Question"/>
            </set>
        </class>
    </hibernate-mapping>

    多的一端:Question類

    package com.exam.entity;
    import java.util.Date;
    public class Question {
        private int questionNo;
        private QuestionType questionType;
        private String questionsTitle;
        public int getQuestionNo() {
            return questionNo;
        }
        public void setQuestionNo(int questionNo) {
            this.questionNo = questionNo;
        }
        public QuestionType getQuestionType() {
            return questionType;
        }
        public void setQuestionType(QuestionType questionType) {
            this.questionType = questionType;
        }
        public String getQuestionsTitle() {
            return questionsTitle;
        }
        public void setQuestionsTitle(String questionsTitle) {
            this.questionsTitle = questionsTitle;
        }
    }

    配置文件:

    <hibernate-mapping package="com.exam.entity">
        <class name="Question" table="exam_question">
            <id name="questionNo" column="question_no" >
                <generator class="increment" />
            </id>
            <many-to-one name="questionType" column="question_type_name"/>
            <property name="questionsTitle" column="questions_title" length="200" />    
        </class>
    </hibernate-mapping>

    首先說明一下一些常用的屬性:

    <many-to-one>元素包含以下屬性:

    name:設定映射的持久化類的屬性名
    column:設定和持久化類的屬性對應的表的外鍵
    class:設定持久化類的屬性的類型
    cascade:設定是否級聯
    lazy:設定是否延遲加載

    <set>元素包含以下屬性:

    name:設定映射的持久化類的屬性名
    cascade:設置是否級聯
    inverse:設定反向控制,如果為true則一的一端不維護外鍵
    <key>:設定與所關聯的持久化類對應的表的外鍵。
    one-to-many:設定所關聯的持久化類

    如果要對一對多關聯映射進行級聯刪除,可以按照上面的舉例進行配置:

    首先看到一的一端:

    <set name="quesion" inverse="true" cascade="delete">
        <key column="question_type_name"/>
        <one-to-many class="Question"/>
    </set>

    這里設置inverse表示一的一端不維護外鍵,設置cascade=”delete”表示刪除一的一端時對關聯到得多的所有的對象也一起刪除

    再看到多的一端:

    <many-to-one name="questionType" column="question_type_name"/>

    這里的column表示外鍵的名,需要和一的一端設置的key標簽里的column保持一致,表示維護同一個鍵值。

    可以按照如下的代碼執行刪除操作:

    session.beginTransaction();

    QuestionType questionType = (QuestionType) session.load(QuestionType.class, "判斷題");            
    session.delete(questionType);        
    session.getTransaction().commit();

    這里使用load查上來的對象是持久狀態的(Persistent),只有是Persistent狀態的對象才可以使用session.delete()操作進行級聯刪除,由new創建的對象屬于Transient狀態,不能進行session.delete()操作。

    posted on 2016-07-09 14:21 youngturk 閱讀(305) 評論(0)  編輯  收藏 所屬分類: 筆試題

    <2016年7月>
    262728293012
    3456789
    10111213141516
    17181920212223
    24252627282930
    31123456

    導航

    統計

    公告

    this year :
    1 jQuery
    2 freemarker
    3 框架結構
    4 口語英語

    常用鏈接

    留言簿(6)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    相冊

    EJB學習

    Flex學習

    learn English

    oracle

    spring MVC web service

    SQL

    Struts

    生活保健

    解析文件

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 免费国产a国产片高清| 在线观看成人免费视频| 伊人婷婷综合缴情亚洲五月| 老外毛片免费视频播放| 国产乱子影视频上线免费观看| 亚洲精品免费网站| 成人a免费α片在线视频网站| 亚洲欧洲日本在线观看| 成人免费无码视频在线网站| 亚洲色欲啪啪久久WWW综合网| 日本免费一区二区三区最新vr| 久久亚洲精品无码gv| 亚洲国产成人久久一区久久| 国产成人1024精品免费| 亚洲AV无码专区电影在线观看| 久久精品一区二区免费看| 亚洲欧洲国产精品久久| 无码人妻久久一区二区三区免费丨| 2020久久精品亚洲热综合一本| 日本免费人成视频播放| eeuss影院免费92242部| 亚洲国产人成在线观看69网站| 99无码人妻一区二区三区免费| 亚洲成AV人片在WWW| 亚洲乱码国产一区网址| 久久久久免费看黄a级试看| 亚洲av无码一区二区三区观看| 全免费一级毛片在线播放| a级毛片免费观看在线| 亚洲系列中文字幕| 国产精品自在自线免费观看| 四虎影视无码永久免费| 亚洲免费中文字幕| 亚洲五月午夜免费在线视频| 99久久综合精品免费| 亚洲av日韩综合一区二区三区| 亚洲精品国产成人片| 免费涩涩在线视频网| 免费人成在线观看网站| 亚洲AV永久无码精品放毛片| 亚洲国产三级在线观看|