解决Hibernate native sql中双冒号(:)转义的问题

在处理签到任务时,遇到查询历史连续最大签到记录的问题。最初试图通过算法解决,但最终选择在数据库层面处理。在使用Hibernate的native SQL查询时,遇到冒号(:)导致的预编译参数错误。通过查看Hibernate源码找到解决方案,避免了重写Hibernate。此博客分享了这一经验,希望对其他开发者有所帮助。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在做一个签到的任务,由于要查询到历史连续最大签到记录的值,起初还是有点迷茫的--有一种想将查询结果查询出来,然后使用算法来解决这个问题。但是折腾了半天,感觉有点难以处理,所以就google了一下和“签到”类似的处理方法,最终参考别人的实现是通过数据库来实现的,具体怎么处理这里不过多介绍了,文章结尾会将链接贴上。

回归到我需要讲解的问题--使用Hibernate 时,native sql中包含冒号(:),由于冒号在hibernate中是一个特殊的符号,用来标识预编译参数变量的.为何我会这么纠结呢,因为我们的系统定位的dao层就是使用Hibernate,所以我只好寻求能够处理好的方式(虽然我知道可以用spring jdbc来分分钟就可以实现),下面看下我初始的sql语句(看不懂也没有关系):


貌似上面的sql语句马上就可以大功告成了,因为我在sqlyog中直接复制进去可以直接将查询结果查询出来,但是当程序跑起来的时候,后台直接抛出一个异常:’Space is not allowed after parameter prefix ':' ‘。

此时,小哥我的内心是崩溃的,问了下度娘,把hibernate的源码找了出来---org.hibernate.engine.query.spi.ParameterParser

public static void parse(String sqlString, Recognizer recognizer) throws QueryException {
		final boolean hasMainOutputParameter = startsWithEscapeCallTemplate( sqlString );
		boolean foundMainOutputParam = false;

		final int stringLength = sqlString.length();
		boolean inQuote = false;
		for ( int indx = 0; indx < stringLength; indx++ ) {
			final 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 ) {
				// skip sending the backslash and instead send then next character, treating is as a literal
				recognizer.other( sqlString.charAt( ++indx ) );
			}
			else {
				if ( c == ':' ) {
					<span style="background-color: rgb(153, 255, 153);">// named parameter--就是这段代码的处理导致的
					final int right = StringHelper.firstIndexOfChar( sqlString, ParserHelper.HQL_SEPARATORS_BITSET, indx + 1 );
					final int chopLocation = right < 0 ? sqlString.length() : right;
					final 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;</span>
				}
				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
						final int right = StringHelper.firstIndexOfChar( sqlString, ParserHelper.HQL_SEPARATORS, indx + 1 );
						final int chopLocation = right < 0 ? sqlString.length() : right;
						final String param = sqlString.substring( indx + 1, chopLocation );
						// make sure this "name" is an integral
						try {
							Integer.valueOf( 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 );
				}
			}
		}
	}
有人说直接改源码,我本来都开始动手了,但是无意间浏览到一个博客---- 解决Hibernate不支持PostgreSQL中双冒号(::)的问题 , 别人是两个冒号,看了下博客的评论,有的小伙伴说可以直接用反斜杠来进行转义,抱着一种将信将疑的态度试了一下,sql如下:

真的是成功了,幸好没有用复杂的方式去解决问题(重写hibernate)。

用hibernate也有几年了,没想到真的是too young too simple ,此博客也希望能给自己的成长留下一个脚印。


最后,文章中说道的签到类型任务的处理实现参考如下(貌似要翻墙哦):

http://blog.itpub.net/29254281/cid-166562-list-1/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值