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仿真分为两步:初始化.仿真循环.仿真是由求解器控制的,求解器主要作用是:计算模块输出.更新模块离散状 ...
随机推荐
- TCP面试题之为什么会有TIME_WAIT状态
1.确保有足够的时间让对方收到ACK包:(一来一去刚好是2MSL) 2.避免新旧连接混淆 MSL(最大报文段的生成时间)在RFC793中规定hi2分钟,实际应用是30秒,1分钟,2分钟等:
- BZOJ2916 [Poi1997]Monochromatic Triangles 数论
答案等于总三角形数-不合法数 一个不合法三角形一定存在两个顶点,在这个三角形中这个顶点的角的两边不同色 #include<cstring> #include<cmath> #i ...
- 2017-2018-2 20179204《网络攻防实践》第十一周学习总结 SQL注入攻击与实践
第1节 研究缓冲区溢出的原理,至少针对两种数据库进行差异化研究 1.1 原理 在计算机内部,输入数据通常被存放在一个临时空间内,这个临时存放的空间就被称为缓冲区,缓冲区的长度事先已经被程序或者操作系统 ...
- 很好用的取代 PuTTY 的SSH远程登陆软件 Termius
一直以来, 我都是用 PuTTY 一个窗口一个窗口来监视我所有的远程服务器. putty-connections-on-a-screen 总感觉非常的不方便, 特别是当远程链接断开需要再重新打开PUT ...
- IntelliJ IDEA如何导入Gradle项目
最近学习Gradle,本文来重点介绍一下,如何使用IntelliJ IDEA导入`Gradle项目的一种方法. 选择菜单栏的File->Open. 在打开的窗口选择对应Gradle项目的buil ...
- [CODEVS1205]单词反转
给出一个英语句子,希望你把句子里的单词顺序都翻转过来 这个题算是第二次做了……第二次用的C++然而还是写不出来 思路1:用一个数组把读过去的单词存起来,再逆序输出即可 思路2:读入句子后,先在句子开头 ...
- Controller方法返回值
1. 返回ModelAndView controller方法中定义ModelAndView对象并返回,对象中可添加model数据.指定view. //入门程序 第一 包类 + 类包 + 方法名 @Re ...
- boost 库的下载和编译_Visual Studio 2013(转)
原文转自 http://blog.csdn.net/lp310018931/article/details/47791143 原文转自 http://m.blog.csdn.net/article/d ...
- mdf 与 mdb的对比
下面的内容从网上搜索而来,未经过本人严格验证,仅供参考. 1.问:mdb数据库能否脱离Access运行?即,没有安装Access,可以打开mdb吗? 答:可以,脱离Access运行,可以到微软的同类产 ...
- 2016北京集训测试赛(九)Problem C: 狂飙突进的幻想乡
Solution 我们发现, 对于一条路径来说, 花费总时间为\(ap + q\), 其中\(p\)和\(q\)为定值. 对于每个点, 我们有多条路径可以到达, 因此对于每个区间中的\(a\)我们可以 ...