简体   繁体   中英

Hibernate Criteria Restrictions.eq is case insensitive

I am using criteria to check if username and password from json are correct. I am sure that input username and password are in correct case but it always returns me object even if the letter case are different.

public Admin getLoggedAdminInformation(String username, String password){

   Criteria criteria = criteria()
            .add(Restrictions.eq("userName", username))
            .add(Restrictions.eq("password", password));

   return (Admin) criteria.uniqueResult();
}

Restrictions.eq just return a SimpleExpression object with ignoreCase is false. So it will create the SQL with the exact same value that you provided.

public static SimpleExpression eq(String propertyName, Object value) {
        return new SimpleExpression(propertyName, value, "=");
    }

So this case insensitive comparison is a behavior of the database. it depends on the database server or table or column configuration.

Ex: in MS SQL server, this is based on the collation of the database, can be overrrieden in table level or column level. sample collation name is Latin1_General_CI_AS , here CI = Case insensitive,AS = accent sensitive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM