Announcement Announcement Module
No announcement yet.
Spring + Hibernate + MySQL + characterEncoding problem Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring + Hibernate + MySQL + characterEncoding problem

    Hi everybody,

    When I save data to DB diacritic in DB looks fine. But when I load the bean from DB and print the properties I get ???.

    The MySQL URL: jdbc:mysql://localhost:3306/mvpdb?setUnicode=true&characterEncoding=ISO-8859-2

    I was also trying to set the sessionFactory hibernateProperties in applicationContext:

    but the result was the same.

    When I create new String like this:
    String name = new String(p.getName().getBytes("ISO-8859-1"), "ISO-8859-2");

    and print it the diacritic is fine. But this is not sexy to implement this construction in every 'String getter()' method.

    Do you have any ideas where and what to set to run the diacritic corectly?

    I'm still implementig bussiness logic and this kind of problems are common in web tire, where I'm not.

    I'm using jdk1.4 and W2K. My applicationContext.xml fragment looks like this:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="mappingResources">
    <property name="hibernateProperties">
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.connection.useUnicode">true</prop>
    <prop key="hibernate.connection.characterEncoding">ISO-8859-2</prop>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName"><value>${jdbc.driverClassNa me}</value></property>
    <property name="url"><value>${jdbc.url}</value></property>
    <property name="username"><value>${jdbc.username}</value></property>
    <property name="password"><value>${jdbc.password}</value></property>

    <bean id="transactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>

    <bean id="roleDAO" class="">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>

    <bean id="personDAO" class="">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>

    <bean id="txProxyTemplate" abstract="true"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager"><ref bean="transactionManager"/></property>
    <property name="transactionAttributes">
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="store*">PROPAGATION_REQUIRED</prop>

    <bean id="mvpService" parent="txProxyTemplate">
    <property name="target">
    <bean class=" pl">
    <property name="roleDAO"><ref bean="roleDAO"/></property>
    <property name="personDAO"><ref bean="personDAO"/></property>

    Thank you for any advice.


  • #2
    MySQL script

    I have solved it by myself... In MySQL I have to create Tables using:
    ENGINE=InnoDB CHARACTER SET='utf8'; at the end...

    CREATE TABLE person_role (
    person_id INT(4) UNSIGNED NOT NULL REFERENCES person(id),
    role_id INT(4) UNSIGNED NOT NULL REFERENCES role(id)

    trhen the encoding will be fine.



    • #3
      i think you can set the character set for the whole db as well depending on what version of mysql you're using.

      Might save having to remember to specify it for each table.