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仿真分为两步:初始化.仿真循环.仿真是由求解器控制的,求解器主要作用是:计算模块输出.更新模块离散状 ...
随机推荐
- 预编译scss以及scss和less px 转rem
预编译scss步骤: 1 搜索ruby并安装,点击 2 安装sass: 3 在hubuilder工具中设置预编译: 触发命令地址为ruby安装地址 命令参数为 %FileName% %FileBase ...
- 写一个Foreach帮助类,在razor中使用
原文发布时间为:2011-05-05 -- 来源于本人的百度文章 [由搬家工具导入] A Better Razor Foreach Loop(razor delegate extension) htt ...
- updatepanel中使用alert弹出框方法
原文发布时间为:2009-05-17 -- 来源于本人的百度文章 [由搬家工具导入] ScriptManager.RegisterStartupScript(this.UpdatePa ...
- [LeetCode] Remove Duplicates from Sorted List II 链表
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- Cflow使用详解【转】
转自:http://blog.csdn.net/hanchaoqi/article/details/40922615 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近使用cflow,根据Cf ...
- ubuntu下使用OBS开斗鱼直播
系统环境:ubuntu 15.10,OBS Studio 0.13.1 OBS是可以在linux,windows,mac下直播的开源软件,官方地址:https://obsproject.com/ 斗鱼 ...
- c# automapper 使用
一.最简单的用法 有两个类User和UserDto 1 public class User 2 { 3 public int Id { get; set; } 4 public string Name ...
- Codeforces Gym100971 L.Chess Match (IX Samara Regional Intercollegiate Programming Contest Russia, Samara, March 13)
这个题就是两个队,看最多能赢的个数,然后比较一下,看两个队是都能赢彼此,还是只有一个队赢的可能性最大.表达能力不好,意思差不多... 和田忌赛马有点像,emnnn,嗯. 代码: 1 #include& ...
- 洛谷——1508 Likecloud-吃、吃、吃
题目背景 问世间,青春期为何物? 答曰:“甲亢,甲亢,再甲亢:挨饿,挨饿,再挨饿!” 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日上课,正当他饿得头昏 ...
- Java命令行的基本编译运行
1.编译 编写MyProgram.java文件,内容如下: public class MyProgram { public static void main(String[] args) { Syst ...