1

I have a problem now. I am using Clear db through Azure. I am not able to store the UTF8 characters to the table. I am using the Spring+Hibernate+Mysql combination.

I can make it work locally by updating the my.cnf file but on Azure it is not allowing me to update because I am using community edition and used by many users. They advised to follow the below point. But I dont know how to set that on hibernate configuration

each time you open a connection to the database, before executing whatever query or queries you want to run on that connection, you should issue the SET NAMES query using the syntax explained at https://dev.mysql.com/doc/refman/5.5/en/set-names.html. So for example to set the charset and collation you want, you could use

SET NAMES utf8 COLLATE utf8_bin;

My hibernate file given below

@Configuration
@EnableTransactionManagement
public class HibernateConfig {


/**
 * Uses an datasource library for local testing with out creating datasources on the server. Not to be used for Production.
 *
 * @return
 */
@Bean
public DriverManagerDataSource jndiDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    try {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    } catch (SQLException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");

    dataSource.setUrl("jdbc:mysql://<azureurl>-f.cloudapp.net:3306/db1");
    dataSource.setUsername("user");
    dataSource.setPassword("pw");




    return dataSource;
}

/**
 * Datasource to be used when running on App servers
 * @return
 * @throws javax.naming.NamingException
 */
/*public @Bean
DataSource jndiDataSource() throws NamingException{
    return (DataSource) new InitialContext().lookup(environment.getProperty("datasourceJndi"));
}
*/
/**
 * @return
 * @throws javax.naming.NamingException
 */
@Bean
public org.springframework.orm.hibernate4.LocalSessionFactoryBean sessionFactory() throws NamingException {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

    Properties prop = new Properties();
    prop.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
    //prop.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
    //Be extremely careful before changing this value to Update or Create
    prop.put("hibernate.hbm2ddl.auto", "none");
    prop.put("hibernate.show_sql", "false");
    prop.put("hibernate.enable_lazy_load_no_trans","true");
    prop.put("hibernate.connection.CharSet","utf8");
    prop.put("hibernate.connection.characterEncoding","utf8");
    prop.put("hibernate.connection.useUnicode",true);

    sessionFactory.setDataSource(this.jndiDataSource());
    sessionFactory.setPackagesToScan(new String[]{"com.sp.domain"});
    sessionFactory.setHibernateProperties(prop);
    return sessionFactory;
}

/**
 * @return
 * @throws javax.naming.NamingException
 */
@Bean
public HibernateTransactionManager transactionManager() throws NamingException {
    HibernateTransactionManager txManager = new HibernateTransactionManager();
    txManager.setDataSource(this.jndiDataSource());
    txManager.setSessionFactory(this.sessionFactory().getObject());
    return txManager;
}
}
Leviand
  • 2,745
  • 4
  • 29
  • 43
Shams
  • 557
  • 8
  • 15

0 Answers0