org.springframework.dao.DataIntegrityViolationException: could not perform addBatch;数据的完整性异常
2014-09-10 16:12
573 查看
在对多个相关的表进行操作时候,比如删除操作,有时候会报异常:DataIntegrityViolationException;
这个时候要考虑在同一个事务里批量操作的SQL语句,在逐条执行时,是否有“在前面删除的数据是否被后面的语句引
用的”情况;如果有的话就会报这个“数据完整性的异常”。
业务需求:
有 “网站表”、“频道表”、”广告位表“,三张表,网站 包含 频道 ,频道 包含 广告位。
在删除一个网站时,也同时删除与之相关联的 频道 和 广告位的信息
网站表:
频道表:
广告位表:
网站service层的代码:
如果是按这个顺序:
先删除网站
在删除频道
最后删除广告位
当先把 网站的记录删除了,在删除 频道 的时候,List<ChannelInfo> channelList = channelInfoDAO.findByProperty("websiteId.id", id);这句会报错。
因为 websiteIdi.id 这条网站的记录已经被删掉了,就找不到这条记录了,所以 websiteId.id 的值就为 NULL ,
报错:
[WARN] 2013-07-16 15:38:01,446 org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1407, SQLState: 72000
[ERROR] 2013-07-16 15:38:01,447 org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ORA-01407: cannot update ("SSP"."TBL_CHANNEL_INFO"."WEBSITE_ID") to NULL
因为 在删除 频道 ,删除广告位的时候 都是需要 网站数据作为参数做基础的,所以这个被关联的 基础数据最后删除即可。只要调换一下顺序。
先删除频道
在删除广告位
最后删除 网站
这个时候要考虑在同一个事务里批量操作的SQL语句,在逐条执行时,是否有“在前面删除的数据是否被后面的语句引
用的”情况;如果有的话就会报这个“数据完整性的异常”。
业务需求:
有 “网站表”、“频道表”、”广告位表“,三张表,网站 包含 频道 ,频道 包含 广告位。
在删除一个网站时,也同时删除与之相关联的 频道 和 广告位的信息
网站表:
频道表:
广告位表:
网站service层的代码:
@Override public void deleteById(Serializable id) throws Exception { try{ // 1 删除网站 websiteInfoDAO.deleteById(id); // 2 删除 频道 List<ChannelInfo> channelList = channelInfoDAO.findByProperty("websiteId.id", id); for(ChannelInfo channel : channelList){ channelInfoDAO.delete(channel.getId()); } // 3 删除 广告位 List<AdspaceInfo> adspaceList = adspaceInfoDAO.findByProperty("websiteId.id", id); for(AdspaceInfo adspace : adspaceList){ adspaceInfoDAO.delete(adspace.getId()); } } catch(Exception e){ e.printStackTrace(); } }
如果是按这个顺序:
先删除网站
在删除频道
最后删除广告位
当先把 网站的记录删除了,在删除 频道 的时候,List<ChannelInfo> channelList = channelInfoDAO.findByProperty("websiteId.id", id);这句会报错。
因为 websiteIdi.id 这条网站的记录已经被删掉了,就找不到这条记录了,所以 websiteId.id 的值就为 NULL ,
报错:
[WARN] 2013-07-16 15:38:01,446 org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1407, SQLState: 72000
[ERROR] 2013-07-16 15:38:01,447 org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ORA-01407: cannot update ("SSP"."TBL_CHANNEL_INFO"."WEBSITE_ID") to NULL
因为 在删除 频道 ,删除广告位的时候 都是需要 网站数据作为参数做基础的,所以这个被关联的 基础数据最后删除即可。只要调换一下顺序。
先删除频道
在删除广告位
最后删除 网站
相关文章推荐
- Hibernate插入数据时,报错:org.springframework.dao.DataIntegrityViolationException: could not insert: [cn.itc
- org.springframework.dao.DataIntegrityViolationException Could not execute JDBC batch update
- 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 delete: [org.jbpm.pvm.internal.model.ExecutionImpl#10001]
- :[org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a];
- org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.
- 【异常】Caused by: java.lang.ClassNotFoundException: org.springframework.dao.DataIntegrityViolationException
- org.springframework.dao.DataIntegrityViolationException: could not insert: [com.front.model.user.Use
- 1、 org.springframework.dao.DataIntegrityViolationException: 将截断字符串或二进制数据。; SQL [n/a]; nested excepti
- org.springframework.dao.InvalidDataAccessResourceUsageException: Could not execute JDBC batch update
- org.springframework.dao.InvalidDataAccessResourceUsageException: Could not execute JDBC batch
- org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query;
- org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exc
- org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exc
- org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query
- 异常积累:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in
- org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query
- org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query;
- org.springframework.dao.InvalidDataAccessResourceUsageException: could not get next sequence value