Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling UTF-8 character set in JSF2.0, Hibernate, MySQL

We are to enable UTF-8 character encoding to our web application designed using JSF2.0, Hibernate, MySQL.

Following is the datasource defined in our application context file

<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />   
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname" />  
        <property name="maxPoolSize" value="10" />
        <property name="maxStatements" value="0" />
        <property name="minPoolSize" value="5" />
        <property name="useUnicode" value="yes" />
        <property name="characterEncoding" value="UTF-8" />
    </bean>

While running the application we are getting exception

Error creating bean with name 'SessionFactory' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Cannot resolve reference to bean 'DataSource'  Error creating bean with name 'DataSource' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Invalid property 'useUnicode' of bean class [com.mchange.v2.c3p0.ComboPooledDataSource]: Bean property 'useUnicode' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

We have also tried using the following but get an error

<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />   
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname?useUnicode=yes&characterEncoding=UTF-8;" />  
        <property name="maxPoolSize" value="10" />
        <property name="maxStatements" value="0" />
        <property name="minPoolSize" value="5" />
    </bean>


Error: The reference to entity "characterEncoding" must end with the ';' delimiter
like image 346
NKS Avatar asked Jan 10 '14 05:01

NKS


People also ask

How do I change MySQL encoding to UTF-8?

To change the character set encoding to UTF-8 for the database itself, type the following command at the mysql> prompt. Replace dbname with the database name: Copy ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci; To exit the mysql program, type \q at the mysql> prompt.

How do I enable utf8?

Select the Configuration Properties > C/C++ > Command Line property page. In Additional Options, add the /utf-8 option to specify your preferred encoding. Choose OK to save your changes.

Does MySQL support UTF-8?

MySQL supports multiple Unicode character sets: utf8mb4 : A UTF-8 encoding of the Unicode character set using one to four bytes per character.

Should I use utf8 or utf8mb4?

The difference between utf8 and utf8mb4 is that the former can only store 3 byte characters, while the latter can store 4 byte characters. In Unicode terms, utf8 can only store characters in the Basic Multilingual Plane, while utf8mb4 can store any Unicode character.


1 Answers

After some work around I am able to handle the issue- Following is the code that is working for me to enable JDBC working with UTF8

<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />   
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname?useUnicode=true&amp;characterEncoding=UTF-8" />  
        <property name="maxPoolSize" value="10" />
        <property name="maxStatements" value="0" />
        <property name="minPoolSize" value="5" />
    </bean>

Using useUnicode=true&amp;characterEncoding=UTF-8 with &amp; served the purpose

To be able to use the same with Hibernate also specify the following to the hibernate properties

<property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>               
            </props>
        </property> 

Specify request encoding format in the filter as well

public class ApplicationFilter implements Filter {    
        public void destroy() {
        }

    public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            request.setCharacterEncoding("utf-8");
        }

        public void init(FilterConfig fConfig) throws ServletException {
        }
    }

Now even if your application and db are not supporting special characters check the database character set , try by recreating the database and using charset UTF8 instead latin1

like image 53
NKS Avatar answered Oct 06 '22 14:10

NKS