JAVA:NamedQuery字符串问题 [英] JAVA: NamedQuery String problem

查看:60
本文介绍了JAVA:NamedQuery字符串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在执行NamedQuery时遇到一些完全匹配的问题.

Hello guys I am having some problems with exact matches while doing a NamedQuery.

我当前正在使用类似这样的东西:

I am currently using something like this:

@NamedQuery(name = MyClass.GET_ENTRY_BY_NAME, query = "select e from Entry e where e.name =:"+ Entry.NAME )

...

Query query = em.createNamedQuery(MyClass.GET_ENTRY_BY_NAME);
        query.setParameter(Entry.NAME, myEntry.getName());

它在大多数情况下都有效,但是我注意到,如果用户传递的文件名末尾带有空格,则namedQuery会忽略该字符.例如:

It works for most cases, however I noticed that in case the user pass the file name with an space at the end, the namedQuery ignores that character. For example:

Query query = em.createNamedQuery(MyClass.GET_ENTRY_BY_NAME);
        query.setParameter(Entry.NAME, myEntry.getName()+ " ");

将返回与之前查询相同的结果.绕过我的有效条目"验证.换句话说,我希望查询完全不返回任何条目,并在以后处理错误.

Will return the same result as the query before. Bypassing my 'valid entry' validation. In other words I'd like the query to return no entry at all and treat the error later on.

我可以想到的一种解决方法是在namedQuery中将参数的单引号引起来,如下所示:

One workaround I could think of, is to put single quotes surrounding my parameter in the namedQuery, like this:

@NamedQuery(name = MyClass.GET_ENTRY_BY_NAME, query = "select e from entry e where e.name =':"+ Entry.NAME "'")

但是,如果字符串中包含单引号,它将浪费我的代码...

However it will trash my code in case the String contains single quotes in it...

有什么想法的人吗?

推荐答案

我在JPA中进行了一些研究,发现它对CH​​ARs进行了一些自动修整,因此我不确定这与Strings的行为是否相同,但是由于它发生在我身上...我相信是的.绕过它的唯一方法是通过在会话DatabaseLogin对象中设置一些属性(请参见

I did some research in JPA and found out that it does some automatic trimming for CHARs, I am not sure if this behaves the same with Strings, but since it is happening to me... I believe so. The only way to bypass it is by setting some attribute within the session DatabaseLogin object (see http://www.eclipse.org/eclipselink/api/1.1/org/eclipse/persistence/sessions/DatabaseLogin.html#setShouldTrimStrings) .

好吧,我不想弄乱会话属性,所以我决定进行某种检查并抛出与NoResultException捕获在我的代码中相同的异常.

Well I didn't want to be messing up with the session properties so I decided to make some sort of check and throwing the same exception as the NoResultException catch does in my code.

我基本上从数据库中获取了结果,并将该字段与我使用的字符串进行了比较:

I basically took the result from the database and compared the field with the String I used:

query.setParameter(Entry.NAME, myEntry.getName());

...

if(!StringUtils.equals(result.getName(), myEntry.getName()){
   do a cool throw just like NoResultException Catch
}

我还必须包含Trim函数axtavt!这只是为了确保如果数据库中的一列具有尾随空格并且与用户给定的参数匹配,则它将作为有效答案包括在内.例如:

I also had to include the Trim function axtavt! This is just to make sure that if the database has a column with trailing spaces and it matches the parameter given by the user, it will be included as a valid answer. For example:

数据库条目:名称="Flavio"-用Function ="Flavio"修饰.

Database entry: Name = "Flavio " - Trimmed with Function = "Flavio".

传递的参数:名称="Flavio"-由JPA自动功能修饰为"Flavio".

Parameter passed: Name = "Flavio " - Trimmed by JPA automatic function = "Flavio".

如果根本不修剪它,只需将"Flavio"与"Flavio"进行比较,并在本应返回该条目的情况下返回NoResult.

If it isnt trimmed at all it will just Compare "Flavio " with "Flavio", returning NoResult when it was supposed to return that Entry.

令人讨厌的解决方法,但是只要没有其他方法可以停止自动修剪,我们就只能利用这种东西.

Nasty workaround, but as long as there is no other way to stop the auto-trimming we will have to just make use of this sort of things.

感谢所有其他答案!

这篇关于JAVA:NamedQuery字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆