Hibernate插入数据时,报错:org.springframework.dao.DataIntegrityViolationException: could not insert: [cn.itc
来源:互联网 发布:淘宝上有卖狗的吗 编辑:程序博客网 时间:2024/05/29 05:56
在用junit测试:插入数据时,报一下错误:
错误原因:
package junit;import org.junit.Test;import cn.itcast.crm.container.ServiceProvinder;import cn.itcast.crm.dao.ISysUserDao;import cn.itcast.crm.domain.SysRole;import cn.itcast.crm.domain.SysUser;import cn.itcast.crm.domain.SysUserGroup;import cn.itcast.crm.util.MD5keyBean;public class TestSysUser {@Testpublic void saveSysUser(){ISysUserDao sysUserDao = (ISysUserDao) ServiceProvinder.getService(ISysUserDao.SERVICE_NAME);SysUser sysUser = new SysUser();sysUser.setName("admin");sysUser.setCnname("系统管理员");SysUserGroup sysUserGroup = new SysUserGroup();sysUserGroup.setId(1);sysUser.setSysUserGroup(sysUserGroup);SysRole sysRole = new SysRole();<span style="color:#ff0000;">sysRole.setId("2c9194ec4b3a2525014b3a2a5a39000");</span>
<span style="white-space:pre"></span><span style="color:#cc0000;">//错误原因:这个id就不存在,数据有问题!</span>sysUser.setSysRole(sysRole);//密码MD5keyBean md5 = new MD5keyBean();sysUser.setPassword(md5.getkeyBeanofStr("123456"));
sysUserDao.save(sysUser);}}<span style="color:#cc0000;"></span>解决方法:
ConstraintViolationException
外键约束问题,
也就是你定义了表的一个外键参照,但是你插入数据的时候
并没有你外键表里根本就没有你刚才插入的数据,
解决办法,现在外键表里插入数据,再把数据插入你实际要插入的表里
以上错误是由于数据的问题,不是代码的问题。
0 0
- Hibernate插入数据时,报错:org.springframework.dao.DataIntegrityViolationException: could not insert: [cn.itc
- org.springframework.dao.DataIntegrityViolationException: could not insert: [com.front.model.user.Use
- org.springframework.dao.DataIntegrityViolationException Could not execute JDBC batch update
- org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update
- org.springframework.dao.DataIntegrityViolationException: could not perform addBatch;数据的完整性异常
- org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert:
- org.springframework.dao.DataIntegrityViolationException:
- org.springframework.dao.DataIntegrityViolationException
- org.springframework.dao.DataIntegrityViolationException
- org.springframework.dao.DataIntegrityViolationException
- org.springframework.dao.DataIntegrityViolationException:
- org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested
- org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested
- :[org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a];
- org.hibernate.ex ception.DataException: could not insert报错
- [hibernate]:org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hi
- org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exce
- org.springframework.dao.DataIntegrityViolationException: Hibernate operation: ORA-00001: unique constraint (CLPM0701.SYS_C007518
- ServiceMetadataBehavior 的 HttpsGetEnabled 属性设置为 True,而 HttpsGetUrl 属性是相对地址,但没有 https 基址。
- Stay Hungry Stay Foolish——网络学习平台分享
- Hibernate——映射关系中的属性详解
- WCF简介
- 五个防护软件之间的对决,结局真不理想……
- Hibernate插入数据时,报错:org.springframework.dao.DataIntegrityViolationException: could not insert: [cn.itc
- 浅谈面向对象和面向过程思维
- C++ 函数参数传递-数组形参
- git 五分钟教程
- 【OpenCV】有关内存释放的一些问题
- 113 鳴谷 李山甫 跌足折齒
- 跨移动终端平台实现
- 【基础练习】【模拟】Uva133 - The Dole Queue题解
- Sort Colors