我在 hibernate 中的 LikeExpression 有过期问题(hibernate 3.5.5)
看起来它不适用于ignorCase。 第一个断言返回 true。第二——假。有什么想法吗?
EntityManager entityManager = (EntityManager) applicationContext.getBean("entityManager");
HibernateTemplate hibernateTemplate = entityManager.getHibernateTemplate();
int size = hibernateTemplate.find("from Source where caption like '%Вход%'").size();
System.out.println("By Query: " + size);
assertTrue(size > 0);
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Source.class);
detachedCriteria.add(Restrictions.like("caption", "Вход", MatchMode.ANYWHERE).ignoreCase());
List list = hibernateTemplate.findByCriteria(detachedCriteria);
System.out.println("By DC: " + list.size());
assertTrue(list.size() > 0);
Looks like it does not work with ignorCase.
The first assert returns true. Second - false. Any Ideas?
EntityManager entityManager = (EntityManager) applicationContext.getBean("entityManager");
HibernateTemplate hibernateTemplate = entityManager.getHibernateTemplate();
int size = hibernateTemplate.find("from Source where caption like '%Вход%'").size();
System.out.println("By Query: " + size);
assertTrue(size > 0);
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Source.class);
detachedCriteria.add(Restrictions.like("caption", "Вход", MatchMode.ANYWHERE).ignoreCase());
List list = hibernateTemplate.findByCriteria(detachedCriteria);
System.out.println("By DC: " + list.size());
assertTrue(list.size() > 0);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
ilike("caption", "Вход", MatchMode.ANYWHERE)
而不是like(..).ignoreCase()
http://www.dil.univ-mrs.fr/ ~massat/docs/hibernate-3.1/api/org/hibernate/criterion/Restrictions.html
Try
ilike("caption", "Вход", MatchMode.ANYWHERE)
instead oflike(..).ignoreCase()
http://www.dil.univ-mrs.fr/~massat/docs/hibernate-3.1/api/org/hibernate/criterion/Restrictions.html
尝试使用
Restrictions.ilike
而不是Restrictions.like
。Try
Restrictions.ilike
instead ofRestrictions.like
.在大多数数据库(Postgres 除外)上,Hibernate 将不区分大小写的
like
执行为lower(caption) like ?
,其中?
是的结果>“%Вход%”.toLowerCase()
。如您所见,在您的情况下,仅当数据库lower()
的结果与String.toLowerCase()
的结果不一致时,它才会产生false
>。例如,如果数据库区域设置配置不支持西里尔字符的大小写转换,则可能会发生这种情况。On most databases (except Postgres) Hibernate executes case-insensitive
like
aslower(caption) like ?
, where?
is a result of"%Вход%".toLowerCase()
. As you can see, in your case it can producefalse
only if results of databaselower()
are inconsistent with results ofString.toLowerCase()
. It can happen, for example, if database locale configuration doesn't support case conversion for Cyrillic characters.