1

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.

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92
Michel
  • 9,220
  • 13
  • 44
  • 59

2 Answers2

3

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)

a1ex07
  • 36,826
  • 12
  • 90
  • 103
  • I was afraid of this answer. There are already a lot of applications deployed on different databases so this solution will cost a lot of time ;) – Michel Jan 30 '12 at 16:14
0

A simple workaround:

    Login login = (Login) getHibernateTemplate().get(Login.class, username);
    if(login != null && !login.getUsername().equals(username)){
        return null;
    }
    return login;   
Michel
  • 9,220
  • 13
  • 44
  • 59