本文介绍了对Hibernate限制中的限制'equals id'区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Hibernate(3.6.9)上对 username 做一个区分大小写的等号,但似乎这个限制不区分大小写。

例如:AdMin或admin都是有效的,但只有admin应该是正确的,否则 .size()应该返回0.我会尽量避免使用。有人得到了不同的解决方案?



代码:

  Session sess = 。了getSessionFactory()的getCurrentSession(); 

@SuppressWarnings(unchecked)
List< Login> logins = sess.createCriteria(Login.class).add(Restrictions.idEq(username))。list();
if(logins.size()== 1){
return logins.get(0);
} else {
return null;
}

Microsoft SQL Server和MySQL在这种情况下并没有什么不同。 / p>

解决方案

我认为您需要更改表格定义。对于Mysqsl,您需要将用户名列更改为 binary或varbinary ,而不是 char / varchar ()。对于SQLServer,确保 username 使用区分大小写的排序规则(请参阅)

I try to do a case sensitive equals on username with Hibernate (3.6.9) but it seems that the restriction is case insensitive.
For example: "AdMin" or "admin" is both valid but only "admin" should be correct and otherwise .size() should return 0. I would try to avoid using like. Somebody got a different solution?

Code:

Session sess = getSessionFactory().getCurrentSession();

@SuppressWarnings("unchecked")
List<Login> logins = sess.createCriteria(Login.class).add(Restrictions.idEq(username)).list();
if(logins.size() == 1) {
    return logins.get(0);
} else {            
    return null;
}

Microsoft SQL Server and MySQL doesn't make a difference in this case.

解决方案

I think you need to change table definition. In case of Mysqsl you need to alter your username column to binary or varbinary instead of char/varchar (http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html). For SQLServer, make sure username uses case-sensitive collation (see list of sql collations)

这篇关于对Hibernate限制中的限制'equals id'区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 17:20