`
limogogogo
  • 浏览: 60485 次
  • 来自: ...
文章分类
社区版块
存档分类
最新评论

ibatis下insert、update、delete操作单元测试通过,发布到tomcat下执行失败

阅读更多
奇怪的问题,折腾了我一上午,分析一下。
ibatis下执行insert、update、delete操作,单元测试通过,发布到tomcat下执行update、delete成功,而insert时却报错,出错信息如下,分析分析。

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: SqlMapClient operation; SQL [];  
--- The error occurred in com/nmgnews/fip/dao/ibatis/maps/FipInfoSort.xml. 
--- The error occurred while applying a parameter map. 
--- Check the insertInfoSort-InlineParameterMap. 
--- Check the statement (update failed). 
--- Cause: java.sql.SQLException: Duplicate entry '06' for key 1; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:  
--- The error occurred in com/nmgnews/fip/dao/ibatis/maps/FipInfoSort.xml. 
--- The error occurred while applying a parameter map. 
--- Check the insertInfoSort-InlineParameterMap. 
--- Check the statement (update failed). 
--- Cause: java.sql.SQLException: Duplicate entry '06' for key 1
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:487)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:440)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)


root cause

org.springframework.dao.DataIntegrityViolationException: SqlMapClient operation; SQL [];  
--- The error occurred in com/nmgnews/fip/dao/ibatis/maps/FipInfoSort.xml. 
--- The error occurred while applying a parameter map. 
--- Check the insertInfoSort-InlineParameterMap. 
--- Check the statement (update failed). 
--- Cause: java.sql.SQLException: Duplicate entry '06' for key 1; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:  
--- The error occurred in com/nmgnews/fip/dao/ibatis/maps/FipInfoSort.xml. 
--- The error occurred while applying a parameter map. 
--- Check the insertInfoSort-InlineParameterMap. 
--- Check the statement (update failed). 
--- Cause: java.sql.SQLException: Duplicate entry '06' for key 1
org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:246)
org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:196)
org.springframework.orm.ibatis.SqlMapClientTemplate.insert(SqlMapClientTemplate.java:367)
com.nmgnews.fip.dao.ibatis.SqlMapFipInfoSortDao.insertInfoSort(SqlMapFipInfoSortDao.java:30)
com.nmgnews.fip.web.spring.EditInfoSortController.handleRequest(EditInfoSortController.java:48)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:857)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:475)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:440)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)


root cause

com.ibatis.common.jdbc.exception.NestedSQLException:  
--- The error occurred in com/nmgnews/fip/dao/ibatis/maps/FipInfoSort.xml. 
--- The error occurred while applying a parameter map. 
--- Check the insertInfoSort-InlineParameterMap. 
--- Check the statement (update failed). 
--- Cause: java.sql.SQLException: Duplicate entry '06' for key 1
com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeUpdate(GeneralStatement.java:91)
com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:447)
com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:82)
org.springframework.orm.ibatis.SqlMapClientTemplate$9.doInSqlMapClient(SqlMapClientTemplate.java:369)
org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:193)
org.springframework.orm.ibatis.SqlMapClientTemplate.insert(SqlMapClientTemplate.java:367)
com.nmgnews.fip.dao.ibatis.SqlMapFipInfoSortDao.insertInfoSort(SqlMapFipInfoSortDao.java:30)
com.nmgnews.fip.web.spring.EditInfoSortController.handleRequest(EditInfoSortController.java:48)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:857)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:475)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:440)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)


note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.

分享到:
评论
13 楼 xwwccsucn 2008-07-04  
cljspn 写道
我也遇到你的问题了,我把索引删除就解决了。不是什么好办法呀,只不过是测试通通了。

fdafafa
12 楼 xwwccsucn 2008-07-04  
cljspn 写道
我也遇到你的问题了,我把索引删除就解决了。不是什么好办法呀,只不过是测试通通了。

fdafafa
11 楼 cljspn 2007-11-09  
我也遇到你的问题了,我把索引删除就解决了。不是什么好办法呀,只不过是测试通通了。
10 楼 li4li5li6 2007-09-18  
人个猜测可能是你在程序中有二次调用update插入重复的数据导致的唯一性约束异常,而Junit中你的update只是调用一次测试通过,如果你调用两次update,看看有没有发生同样的异常,如果有,仔细检查代码,可能会有重复的方法调用。
9 楼 limogogogo 2007-09-18  
由于时间问题,最终换了hibernate,没有进一步查找问题原因。我想最直接的办法是引用ibatis、spring源工程,跟踪程序的执行。
8 楼 limogogogo 2007-07-20  
ddandyy 写道
你只发信息 别人又不知道你是怎么写的  你让别人怎么办   把所有可能都列出来 然后问你是哪一种??

前面一再提到dao测试通过,servlet下测试失败,写java也不是一天两天了,个人猜测很可能不是代码上的问题,而是驱动版本或配置上的问题,因此没有发代码。首先简单介绍一下程序的架构,此程序采用spring+ibatis+mysql。主要jar包spring-2.0.6.jar,mysql-connector-java-3.1.11-bin.jar,ibatis-2.3.0.677.jar,servlet-api-2.4.jar
数据库表:
/*==============================================================*/
/* Table: FIP_InfoSorts                                         */
/*==============================================================*/
create table FIP_InfoSorts
(
   SKey                           varchar(10)                    not null,
  ISKey                          varchar(4),   SName                          varchar(60)                    not null,
   Description                    varchar(512),
   ParentKey                      varchar(2),
   primary key (SKey)
)
comment = "信息分类"
type = InnoDB;

/*==============================================================*/
/* Index: Rel_Sort_Scheme_FK                                    */
/*==============================================================*/
create index Rel_Sort_Scheme_FK on FIP_InfoSorts
(
   ISKey
);

alter table FIP_InfoSorts add constraint FK_Rel_Sort_Scheme foreign key (ISKey)
      references FIP_Input_Schemes (ISKey) on delete restrict on update restrict;
注:虽然有外键引用(ISKey),这里设置允许为null。

ibatis配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap namespace="FipInfoSort">

  <typeAlias alias="infoSort" type="org.limojfip.domain.FipInfoSort"/>

  <select id="getInfoSort"
  parameterClass="java.lang.String"
  resultClass="infoSort">
    SELECT SKey, SName, ParentKey FROM fip_infosorts where SKey = #value#
  </select>

  <insert id="insertInfoSort">
    insert into FIP_InfoSorts (SKey, SName)
    values (#SKey:VARCHAR#, #SName#)
  </insert>

  <update id="updateInfoSort" parameterClass="org.limojfip.domain.FipInfoSort">
    update FIP_InfoSorts
<dynamic prepend="set">
    <isNotNull prepend="," property="isKey">
ISKey=#isKey#
</isNotNull>
<isNotNull prepend="," property="SName">
SName=#SName#
</isNotNull>
<isNotNull prepend="," property="description">
Description=#description#
</isNotNull>
<isNull prepend="," property="description">
Description=null
</isNull>
<isNotNull prepend="," property="parentKey">
ParentKey=#parentKey#
</isNotNull>
<isNull prepend="," property="parentKey">
ParentKey=null
</isNull>
</dynamic>
    where SKey = #SKey#
  </update>
 
  <delete id="deleteInfoSort" parameterClass="java.lang.String">
    delete from FIP_InfoSorts where SKey = #value#
  </delete>

</sqlMap>

junit测试用例:
package org.limojfip.dao.test;

import java.util.List;

import junit.framework.TestCase;

import org.limojfip.dao.FipInfoSortDao;
import org.limojfip.domain.FipInfoSort;

public class SqlMapFipInfoSortDaoTest extends TestCase {

FipInfoSortDao isDao;
protected void setUp() throws Exception {
isDao = TestFactory.getFipFacadeImpl().getInfoSortDao();
}

public void testInsertInfoSort() {
FipInfoSort is = new FipInfoSort();
is.setSKey("61");
is.setSName("test");
is.setDescription("test61");
is.setIsKey(null);
is.setParentKey(null);
isDao.insertInfoSort(is);
}

public void testModifyInfoSort() {
FipInfoSort is = isDao.getInfoSort("61");
is.setSName("test61");
is.setDescription("test");
is.setIsKey("03");
is.setParentKey("03");
isDao.modifyInfoSort(is);
}

public void testDeleteInfoSort() {
isDao.deleteInfoSort("61");
}

}
servlet程序:
package org.limojfip.web.spring;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import org.limojfip.domain.FipActionLog;
import org.limojfip.domain.FipInfoSort;
import org.limojfip.domain.FipInfomation;
import org.limojfip.domain.FipInput;
import org.limojfip.domain.FipInputValue;
import org.limojfip.domain.InputType;
import org.limojfip.domain.logic.FipFacade;

public class EditInfoSortController implements Controller {

private FipFacade fipFacade;

public void setFipFacade(FipFacade fipFacade) {
this.fipFacade = fipFacade;
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String action = request.getParameter("action");
String sKey = request.getParameter("skey");
String sName = request.getParameter("sname");
String description = request.getParameter("description");
String parentKey = request.getParameter("parentkey");
if(parentKey != null && parentKey.length() == 0)
parentKey = null;
if ("3".equals(action)) {
// delete
this.fipFacade.getInfoSortDao().deleteInfoSort(sKey);
response.sendRedirect("ra_01_01.do");
return null;
} else {
FipInfoSort is = new FipInfoSort();
is.setSKey(sKey);
is.setSName(sName);
is.setDescription(description);
is.setParentKey(parentKey);
if ("1".equals(action)) {
// insert
this.fipFacade.getInfoSortDao().insertInfoSort(is);
} else {
// modify
this.fipFacade.getInfoSortDao().modifyInfoSort(is);
}
response.sendRedirect("ra_01_02.do?action" + action
+ "&skey=" + sKey);
return null;
}
}
}
7 楼 simon.yang 2007-07-19  
测试的时候是否对有关联的表插入过数据?可以的话先把表初始化一下。
6 楼 ddandyy 2007-07-19  
你只发信息 别人又不知道你是怎么写的  你让别人怎么办   把所有可能都列出来 然后问你是哪一种??
5 楼 limogogogo 2007-07-19  
最让人奇怪的是单元测试通过,而发布web应用测试失败。junit中的代码与servlet中的代码没有任何区别,只是setXXX()数据来源不同,想不出出错的道理来?而且update、delete在两种测试环境下均通过,而insert只在junit中通过。
4 楼 limogogogo 2007-07-19  
bigpanda 写道
Cause: java.sql.SQLException: Duplicate entry '06' for key 1


这里写的不是很清楚么,table里面某个unique key重复了。


呵呵,我也想知道为什么会重复?前面我已提到“单元测试通过,发布到tomcat下执行update、delete成功,而insert时却报错”。是不是执行了2遍insert操作?
3 楼 bigpanda 2007-07-19  
Cause: java.sql.SQLException: Duplicate entry '06' for key 1


这里写的不是很清楚么,table里面某个unique key重复了。
2 楼 limogogogo 2007-07-19  
没人遇到这样的问题吗?难道真是弱智问题吗?
1 楼 limogogogo 2007-07-19  
是驱动的问题吗?还是ibatis的版本问题?好生奇怪,一定是个弱智问题,朋友们顶顶吧。

相关推荐

    ibatis UPDATE 动态XML与insert

    标题 "ibatis UPDATE 动态XML与insert" 涉及到的是MyBatis框架中对数据库数据进行更新(UPDATE)和插入(INSERT)操作的动态XML配置方式。MyBatis是一个优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射,...

    ibatis应对批量update

    通过上述步骤,我们可以在ibatis中实现高效的批量更新操作,极大地提升系统处理大规模数据的能力。这种方法不仅适用于Java环境下的开发,也适用于其他支持ibatis的编程语言。总之,ibatis提供的批量更新功能是处理...

    ibatis的insert方法的返回值

    ibatis的insert方法的返回值。 介绍了mybaits如何处理返回值的问题。

    spring3学习笔记(2)-集成ibatis3进行单元测试

    在本篇“Spring3学习笔记(2)-集成ibatis3进行单元测试”中,我们将深入探讨如何在Spring3框架中整合Ibatis3,并利用它来进行单元测试。这个主题对于理解如何在实际项目中实现数据访问层的测试具有重要意义。在现代...

    ibatis基本操作数据库

    3. 执行SQL:调用SqlSession的selectList、selectOne、insert、update或delete方法,传入Mapper接口方法的名称和参数。 4. 处理结果:如果是查询操作,返回的结果会被自动映射为Java对象;如果是增删改操作,返回受...

    Ibatis.net的一个简单的测试

    添加和更新操作与删除类似,通过insert和update元素定义SQL语句,然后调用SqlMapper的Insert或Update方法。例如: ```xml &lt;insert id="InsertUser" parameterClass="User"&gt; INSERT INTO Users (Name, Email) ...

    Ibatis测试demo

    4. **参数映射**:使用`@Param`注解传递参数,以及使用`&lt;select&gt;`, `&lt;insert&gt;`, `&lt;update&gt;`, `&lt;delete&gt;`标签进行SQL操作。 5. **结果映射**:配置`&lt;resultMap&gt;`来映射查询结果到Java对象,包括自动类型转换和一对一...

    IBATISDAO库类操作

    `IBATISDAO`库类通常会提供一系列通用的方法,如`selectOne()`, `selectAll()`, `insert()`, `update()`, `delete()`等,这些方法对应于SQL的查询、插入、更新和删除操作。通过泛型机制,`IBATISDAO`可以适应不同的...

    ibatis持久层简单操作

    在IT行业中,持久层框架是数据库操作的重要工具,它简化了数据访问的复杂性,使得开发者可以更加专注于业务逻辑的实现。...通过`ibatisJavaTest`这样的测试文件,我们可以更好地理解和实践Ibatis的各种操作。

    ibatis_test测试

    标题 "ibatis_test测试" 涉及到的是对Ibatis这一知名Java持久层框架的实战测试。Ibatis,作为一个轻量级的ORM(Object-Relational Mapping)框架,允许开发者将SQL语句直接写在配置文件中,使得数据库操作与业务逻辑...

    SPRING + STRUTS + IBATIS测试案例

    在Action或Service中,可以通过SqlMapClient调用SQL语句,执行增删查改操作。 MYSSI可能是项目的主要源代码目录,包含以下组件: 1. **src**: 存放Java源代码,包括Action类、Service接口及实现、DAO接口及实现等。...

    ibatis实现数据的操作

    在Java代码中,我们可以通过SqlSession的`selectOne`、`insert`、`update`和`delete`方法调用对应的Mapper接口方法,执行数据库操作。例如: ```java try (SqlSession session = sqlSessionFactory.openSession()) ...

    iBATIS入门实例测试

    3. 使用`SqlSessionFactory`创建会话工厂,通过`SqlSession`执行SQL操作。 ```java String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); ...

    IBatis常用操作封装

    1. **基本CRUD操作**:`IBatisDao`通常会包含增删改查(Create、Read、Update、Delete)的基本接口,如`insert()`, `select()`, `update()`, `delete()`。这些方法的实现会对应到具体的SQL映射文件,通过`SqlSession...

    <<ibatis框架源代码剖析>> 一个CRUD操作的ibatis项目实例

    本篇文章将通过一个具体的CRUD操作项目实例,帮助读者深入理解iBatis的工作原理和使用方法。 首先,我们需要了解iBatis的核心概念。iBatis的核心是SQL映射文件,它将SQL语句与Java代码分离,使得数据库操作更加清晰...

    ibatis单个对象的各种操作

    在IT行业中,Ibatis是一个非常流行的Java持久层框架,它为开发者提供了灵活的数据库操作方式,使得SQL与Java代码能够紧密结合。本篇文章将主要探讨如何使用Ibatis对单个对象进行各种操作,包括插入、更新、删除和...

    java ibatis main测试

    总的来说,Java iBatis main测试就是通过Java程序的主函数来验证iBatis框架的配置、SQL查询和数据操作的正确性。这种方式对于开发和调试数据库相关的功能非常有用,确保了代码在实际运行前的正确性。

    ibatis简单CRUD例子

    在Ibatis中,CRUD(Create, Read, Update, Delete)是数据库中最基本的操作,下面我们将详细介绍如何使用Ibatis实现这些操作。 1. **创建(Create)**:创建通常涉及到插入新的记录。在Ibatis中,我们定义一个Insert...

    ibatis入门--对数据库的操作

    【ibatis入门--对数据库的操作】这篇文章主要讲解了如何使用iBatis框架来操作数据库,iBatis是一个轻量级的持久层框架,它将SQL语句与Java代码分离,提高了开发效率和代码的可维护性。以下是文章涉及的关键知识点: ...

Global site tag (gtag.js) - Google Analytics