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仿真分为两步:初始化.仿真循环.仿真是由求解器控制的,求解器主要作用是:计算模块输出.更新模块离散状 ...
随机推荐
- 部署私有Docker Registry
安装部署一个私有的Docker Registry是引入.学习和使用Docker这门技术的必经之路之一.尤其是当Docker被所在组织接受,更多人.项目和产品开始接触和使用Docker时,存储和分发自制 ...
- POJ1671 Rhyme Schemes
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1776 Accepted: 984 Special Judge De ...
- Mongoose 参考手册
转自:https://cnodejs.org/topic/548e54d157fd3ae46b233502 Mongoose 是什么? 一般我们不直接用MongoDB的函数来操作MongoDB数据库 ...
- webservice测试工具
webservice测试工具 web service exprlorer
- WebStorm使用JetBrains IDE Support调试
1.安装WebStorm 2.安装谷歌的chome浏览器,并切换到开发者模式 3.下载并安装 JetBrains IDE Support(将2.0.7_0.crx文件直接拖到谷歌浏览器中就会自动安装) ...
- 自定义的类型放入STL的set中,需要重载自定义类中的“<”符号(转)
在以前学习STL的时候,曾经学到过,如果要将自定义的类型放入到set中的话,就需要重载“<”符号,原因是set是一个有序的集合,集合会按照“<”比较的大小,默认按照从小到大的顺序排列.假设 ...
- PHP操作MongoDB(增删改查)
MongoDB的PHP驱动提供了一些核心类来操作MongoDB,总的来说MongoDB命令行中有的功能,它都可以实现,而且参数的格式基本相似.PHP7以前的版本和PHP7之后的版本对MongoDB的操 ...
- javascript 动态添加城市
匿名函数的使用 createTextnode 创建文本 createElement 创建元素 appendChild 将文本或元素追加 <!DOCTYPE html> < ...
- DB2数据库报 [SQL0805N Package "NULLID.SQLLD003" was not found.]
解决办法: cd /home/db2inst1/sqllib/bnddb2 bind @db2cli.lst blocking all grant public sqlerror continue C ...
- 关联分析(Apriori算法)
两个概念: 频繁项集:常出现的物品集合 关联分析:找到诸如:尿布-->啤酒的关联,反过来则是另一条 两个控制参数: 项集的支持度(support):一个项集出现的次数在所有样本中出现的比例 可信 ...