简体   繁体   中英

Convert MySQL latin1_bin with Hibernate to utf-8

I have a MySQL DB (READ-ONLY, NO DB CHANGES) with many varchar fields encoding in latin1_bin. I can not change any DB structure or create any view. I get access to this fields with Hibernate. How can I set the hibernate converting to UTF-8??

<property name="hibernate.connection.characterEncoding">UTF-8</property> 

this don't work.

I don't want to convert any field directly in my application, because that are so much String fields.

Thanks for your Answers!

You can manually change your latin1_bin String to utf-8 String.See sample;

public class StringHelper {
    public static String convertToUTF8(String s) {
        String out = null;
        try {
            out = new String(s.getBytes("ISO-8859-1"), "UTF-8");
        } catch (java.io.UnsupportedEncodingException e) {
            return null;
        }
        return out;
    }

    public static void main(String[] args) {
        String latinString = "héllo";//just sample not sure real latin string or not.
        String utf8String = StringHelper.convertToUTF8(latinString);
        System.out.println(utf8String);

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM