5

How could I integrate Spring with Hibernate using sql server 2005 and have Unicode support. I tried many different ways but I just couldn't get it to work.

Column in the table is nvarchar, character set in Spring is UTF-8. I can read Unicode text (which I added myself using the sql server management tool) just fine but writing doesn't work, it get's gibberished in the DB.

jdbc url is

jdbc:sqlserver://localhost:1433;useUnicode=true;characterEncoding=UTF-8;databaseName=test;

with these properties in the hibernate configuration file

<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.charSet">UTF8</property>

I also have a filter which changes the encoding for all pages

response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF8");

chain.doFilter(request, response);

//do it again, since JSPs will set it to the default
response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF8");

is there some good soul who succeeded in doing so, and can help?

Many thanks!

TheGuyWhoCodes
  • 360
  • 1
  • 3
  • 10
  • If I can't use UTF-8 , it would be good to know how to do this with UCS-2 (or UTF-16..). Is there anything special to do other than to change to charset to UCS-2? – TheGuyWhoCodes Mar 11 '09 at 19:48
  • Check on this, but I don't believe SQL Server supports UTF8. I think the closest approximation you can get is their UCS-2 encoding. See [this MSDN article](http://support.microsoft.com/kb/232580). – duffymo Mar 11 '09 at 18:46
  • Add filter: http://stackoverflow.com/questions/11758094/hibernate-encodes-wrong-while-persisting-objects-utf-8 – alditis Oct 17 '14 at 19:51

3 Answers3

2

Try the suggestion here: http://blog.tremend.ro/2007/05/23/hibernate-utf-8-and-sql-server-2005/

/**
 * Unicode support in SQL Server
 */
public class UnicodeSQLServerDialect extends SQLServerDialect {

    public UnicodeSQLServerDialect() {
        super();

        // Use Unicode Characters
        registerColumnType(Types.VARCHAR, 255, "nvarchar($l)");
        registerColumnType(Types.CHAR, "nchar(1)");
        registerColumnType(Types.CLOB, "nvarchar(max)");

        // Microsoft SQL Server 2000 supports bigint and bit
        registerColumnType(Types.BIGINT, "bigint");
        registerColumnType(Types.BIT, "bit");
    }
}
mshafrir
  • 5,190
  • 12
  • 43
  • 56
2

It seems that you need to change the response and the request encoding in the filter to UTF-8 and all is good!

TheGuyWhoCodes
  • 360
  • 1
  • 3
  • 10
0
public class SQLServerUnicodeDialect extends org.hibernate.dialect.SQLServerDialect {
    public SQLServerUnicodeDialect() {
        super();
        registerColumnType(Types.CHAR, "nchar(1)");
        registerColumnType(Types.LONGVARCHAR, "nvarchar(max)" );
        registerColumnType(Types.VARCHAR, 4000, "nvarchar($l)");
        registerColumnType(Types.VARCHAR, "nvarchar(max)");
        registerColumnType(Types.CLOB, "nvarchar(max)" );

        registerColumnType(Types.NCHAR, "nchar(1)");
        registerColumnType(Types.LONGNVARCHAR, "nvarchar(max)");
        registerColumnType(Types.NVARCHAR, 4000, "nvarchar($l)");
        registerColumnType(Types.NVARCHAR, "nvarchar(max)");
        registerColumnType(Types.NCLOB, "nvarchar(max)");

        registerHibernateType(Types.NCHAR, StandardBasicTypes.CHARACTER.getName());
        registerHibernateType(Types.LONGNVARCHAR, StandardBasicTypes.TEXT.getName());
        registerHibernateType(Types.NVARCHAR, StandardBasicTypes.STRING.getName());
        registerHibernateType(Types.NCLOB, StandardBasicTypes.CLOB.getName() );
    }
}
urbanq
  • 71
  • 1
  • 1