ordinal parameter mismatch
© 版权声明:本文为博主原创文章,转载请注明出处
错误描述:Caused by: org.hibernate.HibernateException: ordinal parameter mismatch
错误代码:
/**
* 查看该黑名单类型是否已存在
*
* @param phone_number
* 用户号码
* @return
*/
public int checkBlack(String phone_number, String call_type, String location_id) { Object[] obj = null;// 参数
String condition = null;// 条件
condition = "phone_number = ? and call_type = ? and location_id = ?";
obj = new Object[] { phone_number, Short.valueOf(call_type), location_id };
List<TphBlack> list = tphBlackDao.find(condition, obj, new OrderBy());
if (list == null || list.size() == 0) {
return 0;// 不存在
} else {
return 1;// 已存在
} }
错误截图:

解决方案:将查询条件中带有"call"的字段放在第一位,使其在语句中的位置在"?"和"="之前。
正确代码:
/**
* 查看该黑名单类型是否已存在
*
* @param phone_number
* 用户号码
* @return
*/
public int checkBlack(String phone_number, String call_type, String location_id) { Object[] obj = null;// 参数
String condition = null;// 条件
// 带有call的字段必须放在第一个=或?之前,否则报错ordinal parameter mismatch
condition = "call_type = ? and phone_number = ? and location_id = ?";
obj = new Object[] { Short.valueOf(call_type), phone_number, location_id };
List<TphBlack> list = tphBlackDao.find(condition, obj, new OrderBy());
if (list == null || list.size() == 0) {
return 0;// 不存在
} else {
return 1;// 已存在
} }
原因分析:查看hibernate-core.jar中的org.hibernate.engine.query包下的ParameterParser.class的源码发现,在parse方法中,将sql语句中的call当做了存储过程中的call关键字。
ParameterParser.class源码
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*
*/
package org.hibernate.engine.query; import org.hibernate.QueryException;
import org.hibernate.hql.classic.ParserHelper;
import org.hibernate.util.StringHelper; /**
* The single available method {@link #parse} is responsible for parsing a
* query string and recognizing tokens in relation to parameters (either
* named, JPA-style, or ordinal) and providing callbacks about such
* recognitions.
*
* @author Steve Ebersole
*/
public class ParameterParser { public static interface Recognizer {
public void outParameter(int position);
public void ordinalParameter(int position);
public void namedParameter(String name, int position);
public void jpaPositionalParameter(String name, int position);
public void other(char character);
} private ParameterParser() {
// disallow instantiation
} /**
* Performs the actual parsing and tokenizing of the query string making appropriate
* callbacks to the given recognizer upon recognition of the various tokens.
* <p/>
* Note that currently, this only knows how to deal with a single output
* parameter (for callable statements). If we later add support for
* multiple output params, this, obviously, needs to change.
*
* @param sqlString The string to be parsed/tokenized.
* @param recognizer The thing which handles recognition events.
* @throws QueryException
*/
public static void parse(String sqlString, Recognizer recognizer) throws QueryException {
boolean hasMainOutputParameter = sqlString.indexOf( "call" ) > 0 &&
sqlString.indexOf( "?" ) < sqlString.indexOf( "call" ) &&
sqlString.indexOf( "=" ) < sqlString.indexOf( "call" );
boolean foundMainOutputParam = false; int stringLength = sqlString.length();
boolean inQuote = false;
for ( int indx = 0; indx < stringLength; indx++ ) {
char c = sqlString.charAt( indx );
if ( inQuote ) {
if ( '\'' == c ) {
inQuote = false;
}
recognizer.other( c );
}
else if ( '\'' == c ) {
inQuote = true;
recognizer.other( c );
}
else {
if ( c == ':' ) {
// named parameter
int right = StringHelper.firstIndexOfChar( sqlString, ParserHelper.HQL_SEPARATORS, indx + 1 );
int chopLocation = right < 0 ? sqlString.length() : right;
String param = sqlString.substring( indx + 1, chopLocation );
if ( StringHelper.isEmpty( param ) ) {
throw new QueryException("Space is not allowed after parameter prefix ':' '"
+ sqlString + "'");
}
recognizer.namedParameter( param, indx );
indx = chopLocation - 1;
}
else if ( c == '?' ) {
// could be either an ordinal or JPA-positional parameter
if ( indx < stringLength - 1 && Character.isDigit( sqlString.charAt( indx + 1 ) ) ) {
// a peek ahead showed this as an JPA-positional parameter
int right = StringHelper.firstIndexOfChar( sqlString, ParserHelper.HQL_SEPARATORS, indx + 1 );
int chopLocation = right < 0 ? sqlString.length() : right;
String param = sqlString.substring( indx + 1, chopLocation );
// make sure this "name" is an integral
try {
new Integer( param );
}
catch( NumberFormatException e ) {
throw new QueryException( "JPA-style positional param was not an integral ordinal" );
}
recognizer.jpaPositionalParameter( param, indx );
indx = chopLocation - 1;
}
else {
if ( hasMainOutputParameter && !foundMainOutputParam ) {
foundMainOutputParam = true;
recognizer.outParameter( indx );
}
else {
recognizer.ordinalParameter( indx );
}
}
}
else {
recognizer.other( c );
}
}
}
} }
ordinal parameter mismatch的更多相关文章
- More x64 assembler fun-facts–new assembler directives(转载)
原文地址 The Windows x64 ABI (Application Binary Interface) presents some new challenges for assembly pr ...
- Known BREAKING CHANGES from NH3.3.3.GA to 4.0.0
Build 4.0.0.Alpha1 ============================= ** Known BREAKING CHANGES from NH3.3.3.GA to 4.0. ...
- ILRuntime 学习
ILRuntime: https://github.com/Ourpalm/ILRuntime Demo: https://github.com/Ourpalm/ILRuntimeU3D 中文在线文档 ...
- 编译原理--05 用C++手撕PL/0
前言 目录 01 文法和语言.词法分析复习 02 自顶向下.自底向上的LR分析复习 03 语法制导翻译和中间代码生成复习 04 符号表.运行时存储组织和代码优化复习 05 用C++手撕PL/0 在之前 ...
- Simulink仿真入门到精通(十) S函数
10.1 S函数概述 S函数也称为Simulink中的系统函数,是用来描述模块的Simulink宏函数,支持M.C等多种语言.当Simulink默认的模块不能满足用户的需求时,用户可以通过S函数自己打 ...
- matlab 调用C程序进行simulink仿真
文章目录 simulink仿真 创建C程序 编译C程序 运行结果 simulink仿真 simulink仿真中需要使用S-Function模块,可以实现调用C程序进行仿真,下面先建立一个简单的仿真: ...
- SmokePing 快速搭建
SmokePing介绍 smokeping是来监控IDC机房网络质量情况,可以从监控图上的延时与丢包情况分辨出机房的网络是否稳定,是否为多线,是否为BGP机房以及到各城市的三个运行商网络各是什么情况. ...
- oracle 启动报ORA-01105 ORA-19808
bash-4.4$ srvctl start instance -i jfcddb2 -d jfcddb PRCR-1013 : Failed to start resource ora.jfcddb ...
- s函数
Matlab 中S-函数模板翻译 10.0 基础知识 (1)Simulink仿真过程 Simulnk仿真分为两步:初始化.仿真循环.仿真是由求解器控制的,求解器主要作用是:计算模块输出.更新模块离散状 ...
随机推荐
- matplotlib pyplot 中文显示问题
import pylab pylab.mpl.rcParams['font.sans-serif'] = ['SimHei'] pylab.mpl.rcParams['axes.unicode_min ...
- linux下常用的日志分析命令【转】
形如下面这样的access.log日志内容: 211.123.23.133 – - [10/Dec/2010:09:31:17 +0800] “GET /query/trendxml/district ...
- Repeater用ul li,一行显示多条数据
原文发布时间为:2009-08-26 -- 来源于本人的百度文章 [由搬家工具导入] .rep { width:680px; float:left; l ...
- updatepanel的使用【他人经验+原创 完整例子】
原文发布时间为:2009-05-16 -- 来源于本人的百度文章 [由搬家工具导入] 刚上传的代码,不知道能不能下载:[源码包含所有ajax控件的简单使用] http://www.xmaspx.com ...
- web.config add handlers and httpmodule to System.Web section.
<?xml version="1.0" encoding="utf-8"?> <!-- For more information on how ...
- js常用方法 备用
/* function obj$(id) 根据id得到对象 function val$(id) 根据id得到对象的值 ...
- 【shell入门】Shell用法
参考:http://www.cnblogs.com/Lynn-Zhang/p/5758287.html 1.sh/bash/csh/Tcsh/ksh/pdksh等shell的区别 sh(全称 Bour ...
- luogu 2463 [SDOI2008]Sandy的卡片 kmp || 后缀数组 n个串的最长公共子串
题目链接 Description 给出\(n\)个序列.找出这\(n\)个序列的最长相同子串. 在这里,相同定义为:两个子串长度相同且一个串的全部元素加上一个数就会变成另一个串. 思路 参考:hzwe ...
- 00深入理解C指针之--- 指针之外
该系列文章源于<深入理解C指针>的阅读与理解,由于本人的见识和知识的欠缺可能有误,还望大家批评指教. C语言从诞生之初就非常善于和硬件打交道,经过这么多年的发展之后,其灵活性和超强的特征是 ...
- window golang1.12.4 安装配置&IDE vscode
step 1:下载golang golang官网地址是 https://golang.org/dl/ 如果没有墙的话,可以用这个地址 https://golang.google.cn/dl/ 如图: ...