Hibernate实现级联删除

版权所有,转载请注明出处zhyiwww@163.com

我的使用背景:

v有新闻和新闻类型,一条新闻只能属于一个类型,一种新闻类型可以对应多条新闻。

v在数据库中,新闻受新闻类型外键约束,并可以级联删除

v在Hibernate中,实现级联删除

vjkakfl


(一)创建数据库表和关系

DROPTABLENewsType;

CREATETABLE NewsType

(

TypeIDNUMBER(10) PRIMARY KEYNOT NULL,

TypeNameVARCHAR2(400) NOT NULL,

DirNameVARCHAR2(400) NOT NULL,

TemplateName VARCHAR2(400) NOT NULL

);

DROPSEQUENCE NEWSTYPE_SEQ;

CREATESEQUENCE NEWSTYPE_SEQINCREMENT BY 1 START WITH 1;


DROPTABLENews;

CREATETABLENews

(

NewsIDNUMBER(10) PRIMARY KEY NOT NULL,

TypeIDNUMBER(10) NOT NULL,

TitleVARCHAR2(400) NOT NULL,

HitCount NUMBER(10) DEFAULT0 NOT NULL,

IssuseDateDATE NOT NULL,

DeployerIDNUMBER(10) NOTNULL,

OriginVARCHAR2(400)NOTNULL,

URLVARCHAR2(400) NOT NULL,

KEYWORDS VARCHAR(400) ,

PriorityNUMBER(10)DEFAULT0 NOT NULL,

SpecialVARCHAR2(400) ,

DescriptionVARCHAR2(400),

AuthorVARCHAR2(400),

TopicVARCHAR2(400)

);

DROP SEQUENCE NEWS_SEQ;

CREATE SEQUENCE NEWS_SEQINCREMENT BY1 START WITH 1;

news-type-relation1.jpg

在oralce中,设置数据表的约束关系如下图:



 (二)
实现数据表到POJO的映射

映射类,我们略去,此处主要显示配置文件

üNews.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- DO NOT EDIT: This is a generated file that is synchronized -->

<!-- by MyEclipse Hibernate tool integration.-->

<!-- Created Fri Nov 11 21:59:47 CST 2005-->

<hibernate-mapping package="com.scenechina.vr.model">

<class name="News" table="NEWS" lazy="false">

<id name="newsid" column="NEWSID" type="java.lang.Long">

<generator>

<param name="sequence">NEWS_SEQ</param>

</generator>

</id>

<property name="typeid" column="TYPEID" type="java.lang.Long" not-null="true"/>

<property name="title" column="TITLE" type="java.lang.String" not-null="true"/>

<property name="hitcount" column="HITCOUNT" type="java.lang.Long" not-null="true"/>

<property name="issusedate" column="ISSUSEDATE" type="java.util.Date" not-null="true"/>

<property name="deployerid" column="DEPLOYERID" type="java.lang.Long" not-null="true"/>

<property name="origin" column="ORIGIN" type="java.lang.String" not-null="true"/>

<property name="url" column="URL" type="java.lang.String" not-null="true"/>

<property name="keywords" column="KEYWORDS" type="java.lang.String"/>

<property name="priority" column="PRIORITY" type="java.lang.Long" not-null="true"/>

<property name="special" column="SPECIAL" type="java.lang.String"/>

<property name="description" column="DESCRIPTION" type="java.lang.String"/>

<property name="author" column="AUTHOR" type="java.lang.String"/>

<property name="topic" column="TOPIC" type="java.lang.String"/>

<set name="newsset" cascade="all" inverse="true" lazy="false" order-by="NEWSDETIALID" table="Newsdetials">

<key column="newsid"/>

<one-to-many/>

</set>

<set name="picset" cascade="all" inverse="true" lazy="false" table="Picture">

<key column="newsid"/>

<one-to-many/>

</set>

<many-to-one name="newstype" column="TYPEID" entity-name="com.model.Newstype" insert="false" update="false"/>

</class>

</hibernate-mapping>


üNewstype.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<!--

Created by the Middlegen Hibernate plugin


http://boss.bekk.no/boss/middlegen/

http://hibernate.sourceforge.net/

-->

<class name="com.scenechina.vr.model.Newstype" table="NEWSTYPE" lazy="false">

<id name="typeid" type="java.lang.Long" column="TYPEID">

<generator/>

</id>

<property name="typename" type="java.lang.String" column="TYPENAME" not-null="true" length="100"/>

<property name="dir" type="java.lang.String" column="DIRNAME" not-null="true" length="100"/>

<property name="templatename" type="java.lang.String" column="TEMPLATENAME" not-null="true" length="100"/>

<!-- associations -->

<set name="news" cascade="all-delete-orphan" inverse="true">

<key column="TYPEID"/>

<one-to-many class="com.model.News"/>

</set>

</class>

</hibernate-mapping>


其实这是个双向以对多关系。


(三)在DAO中实现级联删除

//删除新闻类型记录

dao.getNewsTypeDAO().deleteObjects(typeIDs);

typeID是新闻类型的主键

执行此操作将会删除所有此新闻类型的新闻。

news 是一个Set类型的变量,用来存放此类型的所有的新闻。

(四)删除时遇到的问题

在数据库中,如果设置了约束关系,那么一定要允许级联删除,否则,你在执行删除时会抛出异常。

如果我们的数据库关系设置如下:


news-type-relation2.jpg
 画圈的那个选项没有选上的话
,那么我们在执行的时候就会抛出如下的异常:

006-04-06 13:20:16 ERROR [http-8080-Processor23] (JDBCExceptionReporter.java:72

- ORA-02292: 违反完整约束条件(SC.NEWS_TYPE_RELATIONS) - 已找到子记录日志


rg.hibernate.exception.ConstraintViolationException: could not execute update q

ery

at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.j

va:69)

at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelp

r.java:43)

at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:8

)


问题的关键在如下两点:

1.数据库中要允许数据表的级联删除

2.在映射文件中要将级联删除配置正确