cmschannel.hbm.xml
<hibernate-mapping package="com.thunisoft.cms.entity">
<class name="T_CmsChannel" table="cms_channel" dynamic-insert="true" >
<id name="channelId" type="java.lang.Integer" column="CHANNEL_ID">
<generator class="assigned" />
</id>
<property name="websiteId" column="WEBSITE_ID" type="java.lang.Integer"
not-null="false" />
<property name="configId" column="CONFIG_ID" type="java.lang.Integer"
not-null="false" />
<property name="groupContriId" column="GROUP_CONTRI_ID" type="java.lang.Integer"
not-null="false" />
<property name="groupVisitId" column="GROUP_VISIT_ID" type="java.lang.Integer"
not-null="false" />
<property name="parent" column="PARENT" type="java.lang.Integer"
not-null="false" />
<property name="modelId" column="MODEL_ID" type="java.lang.Integer"
not-null="false" />
<property name="sysType" column="SYS_TYPE" type="java.lang.String"
not-null="false" />
<property name="lft" column="LFT" type="java.lang.Integer"
not-null="false" />
<property name="rgt" column="RGT" type="java.lang.Integer"
not-null="false" />
<property name="name" column="NAME" type="java.lang.String"
not-null="false" />
<property name="docCount" column="DOC_COUNT" type="java.lang.Integer"
not-null="false" />
<property name="priority" column="PRIORITY" type="java.lang.Integer"
not-null="false" />
<property name="hasTitleImg" column="HAS_TITLEIMG" type="java.lang.Integer"
not-null="false" />
<property name="hasChild" column="HAS_CHILD" type="java.lang.Integer"
not-null="false" />
<property name="isDisplay" column="IS_DISPLAY" type="java.lang.Integer"
not-null="false" />
</class>
</hibernate-mapping>在mysql中设置了 parent字段为外键,关联到自身的表
插入时一直报错:Hibernate: insert into cms_channel (WEBSITE_ID, CONFIG_ID, GROUP_CONTRI_ID, GROUP_VISIT_ID, PARENT, MODEL_ID, SYS_TYPE, LFT, RGT, NAME, DOC_COUNT, PRIORITY, HAS_TITLEIMG, HAS_CHILD, IS_DISPLAY, CHANNEL_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2010-01-26 01:12 WARN  o.h.u.JDBCExceptionReporter - SQL Error: 1452, SQLState: 23000
2010-01-26 01:12 ERROR o.h.u.JDBCExceptionReporter - Cannot add or update a child row: a foreign key constraint fails (`db_cms`.`cms_channel`, CONSTRAINT `FK_CHANNEL_PARENT` FOREIGN KEY (`PARENT`) REFERENCES `cms_channel` (`CHANNEL_ID`))
2010-01-26 01:12 ERROR o.h.e.d.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update怎么才能把这个外键关联插入禁掉啊?或者怎么解决这个问题啊?????

解决方案 »

  1.   

    你插入的那条记录的parent,在这个表之前存在的记录里没有id等于这个值得记录
      

  2.   

    插入的parent字段在自身的表(即主表)中存在吗?
      

  3.   

    我在配置文件中怎么没有看到你把parent字体设置为外键呢?<many-to-one>呢?
      

  4.   

    parent字段是哪个表的外键?在配置文件中怎么没有体现?<many-to-one>呢?
      

  5.   

    没有在hbm文件里设置两者的关系
      

  6.   


    我这表里很多外键  而且他们关联到得表 我都不想级联更新  如果hibernate设计成不想用他们还必须写出他们的entity和hbm.xml文件然后再设置他们的关系的话  那么jdbc直连要比他快n倍
      

  7.   


    这个和hibernate配置没有任何关系,即使hibernate配置了关联,你插入一个不存在的parent值一样会报错。也不可能每个地方都设置关系,有的系统,可能设计的时候是针对数据库建模的,可能一个对象的属性都是基本类型,各表之间的关系在程序中自己处理,这样的设计在正式的项目里也是比较常见的