I have a problem. I made a WAB-application by Spring MVC & Hibernate and it has a search. Problem is the search works only with latin symbols when I deploy the WEB-application on openshift. It doesn't work with cyrillic. But it works very well on my local machine. So I have no idea what it can be. I have the same database on openshift that on my local machine and I did a dump of my data and imported it to openshift.
This is the method of search:
public List<News> getNewsWithSubstring(String str) {
Session session = sessionFactory.getCurrentSession();
Criteria crt = session.createCriteria(News.class);
Disjunction disjunction = Restrictions.disjunction();
disjunction.add(Restrictions.like("titleUa", "%" + str + "%"));
disjunction.add(Restrictions.like("titleEn", "%" + str + "%"));
disjunction.add(Restrictions.like("titleRu", "%" + str + "%"));
disjunction.add(Restrictions.like("descriptionUa", "%" + str + "%"));
disjunction.add(Restrictions.like("descriptionEn", "%" + str + "%"));
disjunction.add(Restrictions.like("descriptionRu", "%" + str + "%"));
crt.add(disjunction);
return crt.list();
}
This is the settings of connect to db (from springServlet-context.xml):
<bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.goldcamtech.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="c3p0.idle_test_period">120</prop>
<prop key="hibernate.connection.characterEncoding">utf8</prop>
</props>
</property>
</bean>
<bean name="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.18.97.4:3306/gctech_site?autoReconnect=true&useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
Maybe someone has any idea? Why does it work with latin and cyrillic on my local machine but doesn't work with cyrillic on openshift? Thanks!
Most likely the problem lies in default configuration of MySQL on your local machine and MySQL at OpenShift. I suggest to compare the
CHARACTER SET
on the tables/database. If you didn't specify it explicitly when creating tables/database MySQL will use default value frommy.cnf
See also: Database Character Set and Collation
P.S. Also be warned, that code like this
may work wrong in some situations (for instance, if user's string contains
%
or_
characters) and even more, it may cause SQL injection.