MYSQL case sensitive search (using hibernate) for utf8

百般思念 提交于 2019-12-08 00:40:46

问题


I have Login Table that have utf8 charset and utf8 collation when I want check user name and retrieve other information for this specific user name the hql query give me the same result with lowercase and uppercase. what should l do for my HQL query that work case sesitive I use Mysql 5 and java hibernarte

this is my query:

return queryManager.executeQueryUniqueResult("select b.login from BranchEntity b where b.userName = ?", username);

回答1:


The easiest way is to change your column's definition to use case-insensitive collation like utf8_bin.

Details are here




回答2:


Add class

public class CollateDialect extends MySQLDialect {
    @Override
    public String getTableTypeString() {
        return " COLLATE utf8_bin";
    }
}

and use it:

cfg.setProperty("hibernate.dialect", "org.kriyak.hbm.CollateDialect");

this will make all queries case-sensitive that makes much more sence.



来源:https://stackoverflow.com/questions/1256001/mysql-case-sensitive-search-using-hibernate-for-utf8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!