Announcement Announcement Module
Collapse
No announcement yet.
Spring + Hibernate + MySQL + characterEncoding problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:
    hibernate.connection.characterEncoding
    hibernate.connection.useUnicode

    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">
    <list>
    <value>sk/krajci/mvp/business/hbm/person.hbm.xml</value>
    <value>sk/krajci/mvp/business/hbm/role.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <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>
    </props>
    </property>
    </bean>

    <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>

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

    <bean id="roleDAO" class="sk.krajci.mvp.business.dao.RoleDAO">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>

    <bean id="personDAO" class="sk.krajci.mvp.business.dao.PersonDAO">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>

    <bean id="txProxyTemplate" abstract="true"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager"><ref bean="transactionManager"/></property>
    <property name="transactionAttributes">
    <props>
    <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>
    </props>
    </property>
    </bean>

    <bean id="mvpService" parent="txProxyTemplate">
    <property name="target">
    <bean class="sk.krajci.mvp.business.service.MVPServiceIm pl">
    <property name="roleDAO"><ref bean="roleDAO"/></property>
    <property name="personDAO"><ref bean="personDAO"/></property>
    </bean>
    </property>
    </bean>

    Thank you for any advice.

    MK

  • #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)
    ) ENGINE=InnoDB CHARACTER SET='utf8';

    trhen the encoding will be fine.

    MK

    Comment


    • #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.

      http://dev.mysql.com/doc/mysql/en/alter-database.html

      Comment

      Working...
      X