首页 > Web开发 > 详细

hibernate 删除关联表

时间:2016-07-10 02:01:17      阅读:277      评论:0      收藏:0      [点我收藏+]

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()操作。

hibernate 删除关联表

原文:http://www.blogjava.net/youngturk/archive/2016/07/09/431146.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!