hibernate的级联删除问题

我有两个表一个是news表,一个是newsType表

如下:

CREATETABLENewsType(

TypeIDintPRIMARYKEYNOTNULLidentity(1,1),

TypeNameVARCHAR(400)NOTNULL,

DirNameVARCHAR(400)NOTNULL,

TemplateNameVARCHAR(400)NOTNULL

);

CREATETABLENews

(

NewsIDintidentity(1,1)PRIMARYKEY,

TypeIDintNOTNULLreferencesNewsType(TypeID)ondeletecascade,

TitleVARCHAR(400)NOTNULL,

HitCountintDEFAULT0NOTNULL,

IssuseDatedatetimeNOTNULL,

DeployerIDintNOTNULL,

OriginVARCHAR(400)NOTNULL,

URLVARCHAR(400)NOTNULL,

KEYWORDSVARCHAR(400),

PriorityintDEFAULT0NOTNULL,

SpecialVARCHAR(400),

DescriptionVARCHAR(400),

AuthorVARCHAR(400),

TopicVARCHAR(400)

);

下面是:News.hbm.xml

<?xmlversion="1.0"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

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

<!--

MappingfileautogeneratedbyMyEclipse-HibernateTools

-->

<hibernate-mapping>

<classname="cn.model.mapping.News"table="News"schema="dbo"catalog="demo">

<idname="newsId"type="java.lang.Integer">

<columnname="NewsID"/>

<generatorclass="native"/>

</id>

<!--<propertyname="typeId"column="TypeID"type="java.lang.Integer"not-null="true"/>-->

<many-to-onename="newsType"class="cn.model.mapping.NewsType"fetch="select"insert="false"update="false">

<columnname="TypeID"not-null="true"/>

</many-to-one>

<propertyname="title"type="java.lang.String">

<columnname="Title"length="400"not-null="true"/>

</property>

<propertyname="hitCount"type="java.lang.Integer">

<columnname="HitCount"not-null="true"/>

</property>

<propertyname="issuseDate"type="java.util.Date">

<columnname="IssuseDate"length="23"not-null="true"/>

</property>

<propertyname="deployerId"type="java.lang.Integer">

<columnname="DeployerID"not-null="true"/>

</property>

<propertyname="origin"type="java.lang.String">

<columnname="Origin"length="400"not-null="true"/>

</property>

<propertyname="url"type="java.lang.String">

<columnname="URL"length="400"not-null="true"/>

</property>

<propertyname="keywords"type="java.lang.String">

<columnname="KEYWORDS"length="400"/>

</property>

<propertyname="priority"type="java.lang.Integer">

<columnname="Priority"not-null="true"/>

</property>

<propertyname="special"type="java.lang.String">

<columnname="Special"length="400"/>

</property>

<propertyname="description"type="java.lang.String">

<columnname="Description"length="400"/>

</property>

<propertyname="author"type="java.lang.String">

<columnname="Author"length="400"/>

</property>

<propertyname="topic"type="java.lang.String">

<columnname="Topic"length="400"/>

</property>

</class>

</hibernate-mapping>

NewsType.hbm.xml

<?xmlversion="1.0"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

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

<!--

MappingfileautogeneratedbyMyEclipse-HibernateTools

-->

<hibernate-mapping>

<classname="cn.model.mapping.NewsType"table="NewsType"schema="dbo"catalog="demo">

<idname="typeId"type="java.lang.Integer">

<columnname="TypeID"/>

<generatorclass="native"/>

</id>

<propertyname="typeName"type="java.lang.String">

<columnname="TypeName"length="400"not-null="true"/>

</property>

<propertyname="dirName"type="java.lang.String">

<columnname="DirName"length="400"not-null="true"/>

</property>

<propertyname="templateName"type="java.lang.String">

<columnname="TemplateName"length="400"not-null="true"/>

</property>

<setname="newses"inverse="true"cascade="all-delete-orphan">

<key>

<columnname="TypeID"not-null="true"/>

</key>

<one-to-manyclass="cn.model.mapping.News"/>

</set>

</class>

</hibernate-mapping>

当我测试的时候我要向news表里单独插入数据的时候,就出现异常:

Exceptioninthread"main"org.hibernate.exception.GenericJDBCException:couldnotinsert:[cn.model.mapping.News]

atorg.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)

atorg.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)

atorg.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)

atorg.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1986)

atorg.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2405)

atorg.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)

atorg.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)

atorg.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)

atorg.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)

atorg.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)

atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)

atorg.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)

atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)

atorg.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)

atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)

atorg.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:530)

atorg.hibernate.impl.SessionImpl.save(SessionImpl.java:518)

atorg.hibernate.impl.SessionImpl.save(SessionImpl.java:514)

atcn.model.test.Test.main(Test.java:68)

Causedby:java.sql.SQLException:[Microsoft][SQLServer2000DriverforJDBC][SQLServer]无法将NULL值插入列'TypeID',表'demo.dbo.News';该列不允许空值。INSERT失败。

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

Test.java

packagecn.model.test;

importjava.util.List;

importorg.hibernate.Query;

importorg.hibernate.Session;

importorg.hibernate.Transaction;

importcn.model.HibernateSessionFactory;

importcn.model.mapping.NewsType;

publicclassTest{

publicstaticvoidmain(Stringargs[]){

Sessionsession=HibernateSessionFactory.getSession();

Transactiontx=session.beginTransaction();

Queryquery=session.createQuery("fromNewsTypentwherent.typeId=13");

Listlist=query.list();

NewsTypenewsType=(NewsType)list.get(0);

session.delete(newsType);

session.flush();

session.close();

}

}

相关推荐