论坛首页 入门技术论坛

hibernate的级联删除问题

浏览 4560 次
该帖已经被评为新手帖
作者 正文
   发表时间:2006-12-26  
   我有两个表一个是news表,一个是newsType表
   如下:
    CREATE  TABLE NewsType(
       TypeID  int PRIMARY KEY  NOT NULL identity(1,1) ,
       TypeName  VARCHAR(400) NOT NULL,
       DirName   VARCHAR(400) NOT NULL,
       TemplateName VARCHAR(400) NOT NULL
);
CREATE  TABLE  News
(
       NewsID  int identity(1,1) PRIMARY KEY ,
       TypeID  int NOT NULL references NewsType(TypeID) on delete cascade,
       Title  VARCHAR(400) NOT NULL,
       HitCount int DEFAULT  0 NOT NULL,
       IssuseDate  datetime NOT NULL,
       DeployerID  int NOT  NULL,
       Origin  VARCHAR(400)  NOT  NULL,
      URL  VARCHAR(400) NOT NULL,
       KEYWORDS VARCHAR(400) ,
        Priority  int  DEFAULT  0 NOT NULL,
       Special  VARCHAR(400) ,
       Description  VARCHAR(400),
       Author  VARCHAR(400),
       Topic   VARCHAR(400)

);
下面是:News.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">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="cn.model.mapping.News" table="News" schema="dbo" catalog="demo">
        <id name="newsId" type="java.lang.Integer">
            <column name="NewsID" />
            <generator class="native" />
        </id>
        <!-- <property name="typeId" column="TypeID" type="java.lang.Integer" not-null="true"/> -->
       
        <many-to-one name="newsType" class="cn.model.mapping.NewsType" fetch="select" insert="false" update="false">
            <column name="TypeID" not-null="true" />
        </many-to-one>
        <property name="title" type="java.lang.String">
            <column name="Title" length="400" not-null="true" />
        </property>
        <property name="hitCount" type="java.lang.Integer">
            <column name="HitCount" not-null="true" />
        </property>
        <property name="issuseDate" type="java.util.Date">
            <column name="IssuseDate" length="23" not-null="true" />
        </property>
        <property name="deployerId" type="java.lang.Integer">
            <column name="DeployerID" not-null="true" />
        </property>
        <property name="origin" type="java.lang.String">
            <column name="Origin" length="400" not-null="true" />
        </property>
        <property name="url" type="java.lang.String">
            <column name="URL" length="400" not-null="true" />
        </property>
        <property name="keywords" type="java.lang.String">
            <column name="KEYWORDS" length="400" />
        </property>
        <property name="priority" type="java.lang.Integer">
            <column name="Priority" not-null="true" />
        </property>
        <property name="special" type="java.lang.String">
            <column name="Special" length="400" />
        </property>
        <property name="description" type="java.lang.String">
            <column name="Description" length="400" />
        </property>
        <property name="author" type="java.lang.String">
            <column name="Author" length="400" />
        </property>
        <property name="topic" type="java.lang.String">
            <column name="Topic" length="400" />
        </property>
    </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">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="cn.model.mapping.NewsType" table="NewsType" schema="dbo" catalog="demo">
        <id name="typeId" type="java.lang.Integer">
            <column name="TypeID" />
            <generator class="native" />
        </id>
        <property name="typeName" type="java.lang.String">
            <column name="TypeName" length="400" not-null="true" />
        </property>
        <property name="dirName" type="java.lang.String">
            <column name="DirName" length="400" not-null="true" />
        </property>
        <property name="templateName" type="java.lang.String">
            <column name="TemplateName" length="400" not-null="true" />
        </property>
        <set name="newses" inverse="true" cascade="all-delete-orphan">
            <key>
                <column name="TypeID" not-null="true" />
            </key>
            <one-to-many class="cn.model.mapping.News" />
        </set>
    </class>
</hibernate-mapping>

当我测试的时候  我要向news表里单独插入数据的时候,就出现异常:
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [cn.model.mapping.News]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1986)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2405)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:530)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
at cn.model.test.Test.main(Test.java:68)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]无法将 NULL 值插入列 'TypeID',表 'demo.dbo.News';该列不允许空值。INSERT 失败。

但是,要是同是向两个表里插入数据时,就能成功。是怎么回事啊?

Test.java

package cn.model.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.model.HibernateSessionFactory;
import cn.model.mapping.NewsType;

public class Test {



public static void main(String args[]){

Session session=HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();

Query query=session.createQuery("from NewsType nt where nt.typeId=13");
List list=query.list();
NewsType newsType=(NewsType)list.get(0);
session.delete(newsType);
session.flush();
session.close();

}

}

   发表时间:2006-12-26  
引用
TypeID int NOT NULL references NewsType(TypeID) on delete cascade,

你设置了本字段不准为空,并且外健关联新闻类别表的TypeID字段,你插入一条新闻记录,不设置这个字段那这个字段不就为空了?你自己定义的说不能为空,你以用空值,他不出错就怪了。
0 请登录后投票
   发表时间:2006-12-26  
wtb 写道
引用
TypeID int NOT NULL references NewsType(TypeID) on delete cascade,

你设置了本字段不准为空,并且外健关联新闻类别表的TypeID字段,你插入一条新闻记录,不设置这个字段那这个字段不就为空了?你自己定义的说不能为空,你以用空值,他不出错就怪了。
那你说应该怎么改呢?
小弟刚刚学hibernate.
但是,当我删除的时候,不出现异常,可是数据库里的数据根本就没有被删除。不知道是怎么回事
0 请登录后投票
   发表时间:2006-12-27  
哪个字段可以为空值,那么你在数据库中就应该设置允许为空,并且在相应的hbm.xml文件中的property块中的not-null设为false。
删除不了,你先看看log4j的debug信息显示了什么,有没有执行到数据库的delete语句,如果没有,有可能你没有执行session的commit。
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics