首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

org.springframework.dao.DataIntegrityViolationException:

2011-11-11 
Hibernate 一对多关联的问题想测试一下两个表数据的保存,其中表Employee引用了Department表的主键作为外键

Hibernate 一对多关联的问题
想测试一下两个表数据的保存,其中表Employee引用了Department表的主键作为外键

Department的hbm文件:

        <class   name= "com.sunny.vo.Department "   table= "department "   schema= "dbo "   catalog= "test ">
                <id   name= "id "   type= "java.lang.Integer ">
                        <column   name= "id "   />
                        <generator   class= "identity "> </generator>
                </id>
                <property   name= "dname "   type= "java.lang.String ">
                        <column   name= "dname "   length= "10 "   />
                </property>
                <set   name= "employees "   inverse= "true ">
                        <key>
                                <column   name= "pid "   not-null= "true "   />
                        </key>
                        <one-to-many   class= "com.sunny.vo.Employee "   />
                </set>
        </class>

Employee的hbm文件:

<hibernate-mapping>
        <class   name= "com.sunny.vo.Employee "   table= "employee "   schema= "dbo "   catalog= "test ">
                <id   name= "id "   type= "java.lang.Integer ">
                        <column   name= "id "   />
                        <generator   class= "identity "   />
                </id>
                <many-to-one   name= "department "   class= "com.sunny.vo.Department "   fetch= "select ">
                        <column   name= "pid "   not-null= "true "   />
                </many-to-one>
                <property   name= "name "   type= "java.lang.String ">
                        <column   name= "name "   length= "10 "   />
                </property>
        </class>


</hibernate-mapping>

在测试时显示出错:
org.springframework.dao.DataIntegrityViolationException:   Hibernate   operation:   could   not   insert:   [com.sunny.vo.Employee];   SQL   [insert   into   test.dbo.employee   (pid,   name)   values   (?,   ?)   select   scope_identity()];   [Microsoft][SQLServer   2000   Driver   for   JDBC][SQLServer]INSERT   语句与   COLUMN   FOREIGN   KEY   约束   'FK_employee_department '   冲突。该冲突发生于数据库   'test ',表   'department ',   column   'id '。;   nested   exception   is   java.sql.SQLException:   [Microsoft][SQLServer   2000   Driver   for   JDBC][SQLServer]INSERT   语句与   COLUMN   FOREIGN   KEY   约束   'FK_employee_department '   冲突。该冲突发生于数据库   'test ',表   'department ',   column   'id '。
java.sql.SQLException:   [Microsoft][SQLServer   2000   Driver   for   JDBC][SQLServer]INSERT   语句与   COLUMN   FOREIGN   KEY   约束   'FK_employee_department '   冲突。该冲突发生于数据库   'test ',表   'department ',   column   'id '。

是哪里出错了?请帮忙了

[解决办法]
在插入时候发生了外键约束,
不存在你向employee表中插入数据时对应的department.
[解决办法]
当然报错拉,在Employee的hbm文件:
<many-to-one name= "department " class= "com.sunny.vo.Department " fetch= "select ">
<column name= "pid " not-null= "true " />
</many-to-one>
你设置他为只select的,插入数据肯定报错了...
改为:fetch= "select,insert,update,delete " 就可以操作所有的了
建议你去看看Hibernate基础语法~
[解决办法]
奉劝不要用一对多之类的,很难掌握他的性能情况

热点排行