How to change varchar to nvarchar in mssql using hibernate

In my recent assignment I was asked to change how hibernate creates a table changing varchar fields to nvarchar. The problem is in MSSQL we need an nvarchar to support UTF-8 characters.

We are actually using a JDBC MSSQL driver available here:
http://technet.microsoft.com/en-us/library/ms378422.aspx

This driver needs to be configured inside jboss, the server I'm on.

First you need to extend SQLServerDialect and override the VARCHAR definition.
package org.kbs.hibernate.dialect;

import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.dialect.SQLServerDialect;

public class MSSQLServerDialect extends SQLServerDialect {

public UnicodeSQLServerDialect() {
super();

registerColumnType(Types.VARCHAR, "nvarchar($l)");
registerColumnType(Types.BIGINT, "bigint");
registerColumnType(Types.BIT, "bit");
}

}

Second you need to tell hibernate to use your new class:
<property name="hibernate.dialect" value="org.kbs.hibernate.dialect.MSSQLServerDialect" />

Reference:
http://grepcode.com/file/repo1.maven.org/maven2/org.hibernate/hibernate-core/3.3.1.GA/org/hibernate/dialect/SQLServerDialect.java

0 nhận xét:

Đăng nhận xét