Hibernate查询中的Unicode字符串 [英] Unicode String in Hibernate Queries

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

问题描述

在SQL中,可以编写一个查询来查找这样一个人的名字:

  SELECT * FROM Person P WHERE P.Name LIKE N'%ike%'

这个查询可以使用unicode字符运行(假设名称列和数据库被设置为处理unicode支持)。

我在HQL(NHibernate)运行的HQL中有类似的查询。生成的查询如下所示:

  SELECT P FROM SumTotal.TP.Models.Party.Person P join P.Demographics PD WHERE PD.LastName LIKE'%カタカ%')

不幸的是,将'N'放在HQL中的文字会导致错误。我试过在字符串中转义unicode字符,但仍然没有成功。



数据库正在接受和保存Hibernate的Unicode字符。我已经能够使用unicode字符串成功填充对象,使用Hibernate保存它,并在数据库中验证它。在我看来,有一点奇怪,我不能在自定义查询中使用unicode字符串(或者我也假设命名查询)。



这是已知问题还是Hibernate(Nhibernate)的限制?你如何在HQL中使用unicode?

有几个网站建议使用Criteria查询。由于在我工作的框架中有一些限制,这是不可能的。 解决方案

/ p>

  IList< Person> people = session 
.CreateQuery(from Person p where p.Name like:name)
.SetParameter(name,%カタカ%)
.List< Person>( );

它们也有利于保护您的查询免受SQL注入攻击。


In SQL one can write a query that searches for a name of a person like this:

SELECT * FROM Person P WHERE P.Name LIKE N'%ike%'

This query would run with unicode characters (assuming that the Name column and database were setup to handle unicode support).

I have a similar query in HQL that is run by Hibernate (NHibernate). The generated query looks like:

SELECT P FROM SumTotal.TP.Models.Party.Person P join P.Demographics PD WHERE (PD.LastName LIKE '%カタカ%'  )

Unfortunately, placing a 'N' in front of the literal in the HQL results in an error. I've tried escaping the unicode characters in the string and still no success.

The database is accepting and saving unicode characters from Hibernate. I've been able to successfully populate an object with a unicode string, save it with Hibernate, and verify it in the database. It would seem to me as a little odd that I cannot use unicode strings in custom queries (or I'm also assuming named queries).

Is this a known issue or limitation of Hibernate (Nhibernate)? How do you use unicode in HQL?

Several sites suggest using the Criteria queries. Due to constraints in the framework that I'm working in, this is not possible.

解决方案

Have you tried with parameters:

IList<Person> people = session
    .CreateQuery("from Person p where p.Name like :name")
    .SetParameter("name", "%カタカ%")
    .List<Person>();

They also have the advantage to protect your query against SQL injection.

这篇关于Hibernate查询中的Unicode字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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