标签:
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>
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>
<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()操作。
标签:
原文地址:http://www.cnblogs.com/hanxue53/p/4193362.html