DataIntegrityViolationException将数据类型nvarchar转换为int时出错

时间:2017-06-30 17:22:59

标签: java spring jdbc types

我有这个问题,但一切似乎都配置得当,所以我问自己是否有一些新鲜的眼睛可能会看到我正在犯的错误。

我正在一个表中进行更新,该表中包含四列id和一些其他数据。我正在使用的存储过程如下:

ALTER PROCEDURE [dbo].[sp_UPLOAD_MOVTIT_Update]
( @IN_ID_RECORD             int
, @IN_COD_IST               nvarchar(5)
, @IN_COD_SOC_GEST          char(5)
, @IN_COD_PTF               nvarchar(2)
, @IN_COD_LIN               nvarchar(2)
, @IN_COD_GEST              nvarchar(5)
, @IN_DAT_RIF               nvarchar(10)    
, @IN_DAT_VAL               nvarchar(10)
, @IN_ID_UPLOAD             int
, @IN_COD_CAUS              nvarchar(6)
, @IN_VAL_COD_TIT           nvarchar(30)
, @IN_DIV_RIF               nvarchar(3)
, @IN_IMP                   numeric(25,3)
, @IN_IMP_RATEO             decimal(18,3)
, @IN_QTA                   numeric(18,5)
, @IN_PRZ                   decimal(21, 10) = 0
, @IN_FX                    decimal(18, 9)
, @IN_COD_MER               nvarchar(20)
, @IN_NOTE                  nvarchar(100))


AS      


DECLARE @@SQL                   nvarchar(max)
    , @@TABLE_NAME              nvarchar(45)
    , @@WHERE                   nvarchar(max)
    , @@DAT_RIF                 datetime    
    , @@DAT_VAL                 datetime

SET NOCOUNT ON   

SET @@TABLE_NAME = 'UPLOAD_MOVTIT'
SET @@TABLE_NAME = @@TABLE_NAME + '_' + @IN_COD_SOC_GEST 

SET @@WHERE = ' WHERE'

IF (@IN_ID_RECORD <> 0)
    SET @@WHERE = @@WHERE + ' AND ID_RECORD = @IN_ID_RECORD'    

IF (LTRIM(@IN_COD_IST) <> '')       
    SET @@WHERE = @@WHERE + ' AND COD_IST = @IN_COD_IST'

IF (LTRIM(@IN_COD_SOC_GEST) <> '')
    SET @@WHERE = @@WHERE + ' AND COD_SOC_GEST = @IN_COD_SOC_GEST'  

--IF (LTRIM(@IN_COD_PTF) <> '')
--  SET @@WHERE = @@WHERE + ' AND COD_PTF = @IN_COD_PTF'    

--IF (LTRIM(@IN_COD_LIN) <> '')     
--  SET @@WHERE = @@WHERE + ' AND COD_LIN = @IN_COD_LIN'

--IF (LTRIM(@IN_COD_GEST) <> '')        
--  SET @@WHERE = @@WHERE + ' AND COD_GEST = @IN_COD_GEST'  

IF (LTRIM(@IN_DAT_RIF) <> '')
    SET @@DAT_RIF = CONVERT(SMALLDATETIME, @IN_DAT_RIF, 103)    

IF (LTRIM(@IN_DAT_VAL) <> '')
    SET @@DAT_VAL = CONVERT(SMALLDATETIME, @IN_DAT_VAL, 103)    

IF (@IN_ID_UPLOAD <> 0)
    SET @@WHERE = @@WHERE + ' AND ID_UPLOAD = @IN_ID_UPLOAD'    

IF (@@WHERE <> ' WHERE')        
    SET @@WHERE = REPLACE(@@WHERE, ' WHERE AND', ' WHERE')
ELSE
    SET @@WHERE = ''

BEGIN
    SET @@SQL = 'UPDATE ' + @@TABLE_NAME 
        + ' SET COD_PTF = @IN_COD_PTF
            , COD_LIN = @IN_COD_LIN
            , COD_GEST = @IN_COD_GEST
            , DAT_RIF = ' + @@DAT_RIF + '
            , DAT_VAL = ' + @@DAT_VAL + '
            , COD_CAUS = @IN_COD_CAUS
            , VAL_COD_TIT = @IN_VAL_COD_TIT
            , DIV_RIF = @IN_DIV_RIF
            , IMP = @IN_IMP
            , IMP_RATEO = @IN_IMP_RATEO
            , QTA = @IN_QTA
            , PRZ = @IN_PRZ
            , FX = @IN_FX
            , COD_MER = @IN_COD_MER
            , NOTE = @IN_NOTE
            , DAT_ULT_MOD = CURRENT_TIMESTAMP ' + @@WHERE 

END

PRINT @@sql

EXEC sp_executesql @@SQL
                    , N'@IN_ID_RECORD int, @IN_COD_IST  nvarchar(5), @IN_COD_SOC_GEST char(5), @IN_COD_PTF nvarchar(2), @IN_COD_LIN nvarchar(2), @IN_COD_GEST nvarchar(5), @IN_DAT_RIF nvarchar(10), @IN_DAT_VAL nvarchar(10), @IN_COD_CAUS nvarchar(6), @IN_VAL_COD_TIT nvarchar(30), @IN_DIV_RIF  nvarchar(3), @IN_ID_UPLOAD int, @IN_IMP numeric(25,3), @IN_IMP_RATEO decimal(18,3), @IN_QTA numeric(18,5), @IN_PRZ  decimal(21, 10), @IN_FX decimal(18, 9), @IN_COD_MER nvarchar(20), @IN_NOTE nvarchar(100)'
                    , @IN_ID_RECORD, @IN_COD_IST, @IN_COD_SOC_GEST, @IN_COD_PTF, @IN_COD_LIN, @IN_COD_GEST, @IN_DAT_RIF, @IN_DAT_VAL, @IN_COD_CAUS, @IN_VAL_COD_TIT, @IN_DIV_RIF, @IN_ID_UPLOAD, @IN_IMP, @IN_IMP_RATEO, @IN_QTA, @IN_PRZ, @IN_FX, @IN_COD_MER, @IN_NOTE

RETURN @@ERROR

我知道这是一长串参数,但这正是我想要解决的问题。存储通过spring JDBC模板映射。这是实施:

package com.sgss.nove.domain.storedprocedure.upload;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlReturnResultSet;
import org.springframework.jdbc.object.StoredProcedure;

import com.sgss.nove.domain.model.upload.UploadMovTitSave;

public class UploadMovTitUpdateStoredProcedure extends StoredProcedure {

    private static final String SPROC_NAME = "sp_UPLOAD_MOVTIT_Update";

    public UploadMovTitUpdateStoredProcedure(JdbcTemplate jdbcTemplate) {
        super(jdbcTemplate, SPROC_NAME);    


        declareParameter(new SqlReturnResultSet("result", new RowMapper<UploadMovTitSave>() {

            @Override
            public UploadMovTitSave mapRow(ResultSet rs, int rowNum) throws SQLException {
                UploadMovTitSave movTit = new UploadMovTitSave();



                return movTit;


            }
        }));

            declareParameter(new SqlParameter("IN_ID_RECORD", Types.INTEGER));      
            declareParameter(new SqlParameter("IN_COD_IST", Types.VARCHAR));
            declareParameter(new SqlParameter("IN_COD_SOC_GEST", Types.VARCHAR));
            declareParameter(new SqlParameter("IN_ID_UPLOAD", Types.INTEGER));
            declareParameter(new SqlParameter("IN_COD_PTF", Types.VARCHAR));
            declareParameter(new SqlParameter("IN_COD_LIN", Types.VARCHAR));
            declareParameter(new SqlParameter("IN_COD_GEST", Types.VARCHAR));
            declareParameter(new SqlParameter("IN_DAT_RIF", Types.VARCHAR));
            declareParameter(new SqlParameter("IN_DAT_VAL", Types.VARCHAR));
            declareParameter(new SqlParameter("IN_COD_CAUS", Types.VARCHAR));
            declareParameter(new SqlParameter("IN_VAL_COD_TIT", Types.VARCHAR));
            declareParameter(new SqlParameter("IN_DIV_RIF", Types.VARCHAR));
            declareParameter(new SqlParameter("IN_IMP", Types.NUMERIC));
            declareParameter(new SqlParameter("IN_IMP_RATEO", Types.DECIMAL));
            declareParameter(new SqlParameter("IN_QTA", Types.NUMERIC));
            declareParameter(new SqlParameter("IN_PRZ", Types.DECIMAL));
            declareParameter(new SqlParameter("IN_FX", Types.DECIMAL));
            declareParameter(new SqlParameter("IN_COD_MER", Types.VARCHAR));
            declareParameter(new SqlParameter("IN_NOTE", Types.VARCHAR));

            compile();

    }
}

该实现由dao中的方法调用,即:

@Override
public UploadMovTitSave updateMovTitDetail(UploadMovTitSave entity) {
    UploadMovTitUpdateStoredProcedure sproc = new UploadMovTitUpdateStoredProcedure(jdbcTemplate);
    HashMap<String, Object> params = new HashMap<String, Object>(); 
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setDecimalSeparator('.');
    String pattern = "###0.0#";
    DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);
    decimalFormat.setParseBigDecimal(true);
    try {
    params.put("IN_ID_RECORD", (Integer)entity.getIdRecord());
    params.put("IN_COD_IST", (entity.getCodIst()!=null ? entity.getCodIst() : EMPTY_STRING));
    params.put("IN_COD_SOC_GEST", (entity.getCodSocGest()!=null ? entity.getCodSocGest() : EMPTY_STRING));
    params.put("IN_COD_PTF", (entity.getCodPtf()!=null ? entity.getCodPtf() : EMPTY_STRING));
    params.put("IN_COD_LIN", (entity.getCodLin()!=null ? entity.getCodLin() : EMPTY_STRING));
    params.put("IN_COD_GEST", (entity.getCodGest()!=null ? entity.getCodGest() : EMPTY_STRING));
    params.put("IN_DAT_RIF", (entity.getDatRif()!=null ? entity.getDatRif() : EMPTY_STRING));
    params.put("IN_DAT_VAL", (entity.getDatVal()!=null ? entity.getDatVal() : EMPTY_STRING));
    params.put("IN_ID_UPLOAD", (Integer)entity.getIdUpload());
    params.put("IN_COD_CAUS", (entity.getCodCaus()!=null ? entity.getCodCaus() : EMPTY_STRING));
    params.put("IN_VAL_COD_TIT", (entity.getValCodTit()!=null ? entity.getValCodTit() : EMPTY_STRING));
    params.put("IN_DIV_RIF", (entity.getDivRif()!=null ? entity.getDivRif() : EMPTY_STRING));
    params.put("IN_IMP", (BigDecimal) decimalFormat.parse(entity.getImp()));
    params.put("IN_IMP_RATEO", (BigDecimal) decimalFormat.parse(entity.getImpRateo()));
    params.put("IN_QTA", (BigDecimal) decimalFormat.parse(entity.getQta()));
    params.put("IN_PRZ", (BigDecimal) decimalFormat.parse(entity.getPrz()));
    params.put("IN_FX", (BigDecimal) decimalFormat.parse(entity.getFx()));
    params.put("IN_COD_MER", (entity.getCodMer()!=null ? entity.getCodMer() : EMPTY_STRING));
    params.put("IN_NOTE", (entity.getNote()!=null ? entity.getNote() : EMPTY_STRING));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        log.debug("Couldn't parse BigDecimals");
    }
    log.error("updateMovTitDetail - parametri input : " + params.toString());

    Map<String, Object> results = sproc.execute(params);


    UploadMovTitSave movTit = (UploadMovTitSave) results.get("result");


    return movTit;
}

dao使用Hibernate实体的等价物,它是一个名为UploadMovTitSave的公共bean,代码如下:

public class UploadMovTitSave implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 7281618006516386370L;

private Integer idRecord;

private String codIst;
private String codSocGest;
private String codPtf;
private String codLin;
private String codGest;
private String datRif;
private String datVal;
private Integer idUpload;
private String codCaus;
private String valCodTit;

private String divRif;
private String imp;
private String impRateo;
private String qta;

private String prz;
private String fx;
private String codMer;
private String note;

//Getters and setters methods

任何人都可以看到类型匹配的错误吗?我的意思是,只要我能看到类型匹配,但是我整天都在使用这段代码,所以我可能错了。

错误如下:

org.springframework.dao.DataIntegrityViolationException: CallableStatementCallback; SQL [{call sp_UPLOAD_MOVTIT_Update(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}]; Error converting data type nvarchar to int.; nested exception is java.sql.SQLException: Error converting data type nvarchar to int.

1 个答案:

答案 0 :(得分:0)

问题在this文档页面中描述,sql类型integer映射到java类型int