SQL Server + hbm2ddl + unicode columns
Hibernate offers org.hibernate.dialect.SQLServerDialect as the dialect for SQL Server. When generating the database schema, using hbm2ddl, the string type columns do not support native characters. So the following mapping:
<property name="title" length="128" />
will produce the following SQL:
... title varchar(128) null, ...
By extending the org.hibernate.dialect.SQLServerDialect we can achieve the generation of NCHAR, NVARCHAR, and NTEXT columns instead of CHAR, VARCHAR and TEXT.
package com.foo.hibernate;
import java.sql.Types;
import org.hibernate.dialect.SQLServerDialect;
public class SQLServerNativeDialect extends SQLServerDialect{
public SQLServerNativeDialect() {
super();
registerColumnType(Types.CHAR, "nchar(1)");
registerColumnType(Types.VARCHAR, "nvarchar($l)");
registerColumnType(Types.LONGVARCHAR, "nvarchar($l)");
registerColumnType(Types.CLOB, "ntext");
}
}
All we need to do now is plug this dialect in our hibernate configuration:
<property name="hibernate.dialect"> com.foo.hibernate.SQLServerNativeDialect </property>
Related hibernate forums thread: http://forum.hibernate.org/viewtopic.php?t=972518
Related API method: http://www.hibernate.org/hib_docs/v3/api/org/hibernate/dialect/Dialect.html
May 1st, 2009 at 5:19
Does anyone know if there is another language or set of commands beside SQL for talking with databases?
I’m working on a project and am doing some research thanks
May 6th, 2011 at 7:41
its not working for me…I extended the oracle dialect ans in some places i want String variable to created in database as nvarchar. I tried the same way as you told ablobe but its not working for me.
July 14th, 2011 at 17:14
Ioannis,
Thanks! Having use of NTEXT etc is very, very useful for me and my Hibernate/SQL Server fun :)
I don’t normally do much software engineering and it’s really pulling my hair out!
Thanks again,
Josh.