A good post on registerHibernateType

From: http://www.databaseskill.com/498002/

First of all, custom dialect class – Hibernate Dialect class need to inherit the database we use the dialect class. For example: if we use MySQL (version 5.xx), we need to inherit of the org.hibernate dialect. MySQL5Dialect, “; if we are using DB2, then we should inherit” org.hibernate.dialect.DB2Dialect “; And I use SqlServer2008, so I have to inheritance org.hibernate.dialect.SQLServerDialect, the following reference code:

Java code
  1. import java.sql.Types;
  2. import org.hibernate.Hibernate;
  3. import org.hibernate.dialect.SQLServerDialect;
  4. public   class SqlServer2008Dialect extends SQLServerDialect {
  5.      the public SqlServer2008Dialect () {
  6.          super ();
  7. registerHibernateType (Types.CHAR, Hibernate.STRING.getName ());
  8. registerHibernateType (Types.NVARCHAR, Hibernate.STRING.getName ());
  9. registerHibernateType (Types.LONGNVARCHAR, Hibernate.STRING.getName ());
  10. registerHibernateType (Types.DECIMAL, Hibernate.DOUBLE.getName ());
  11. }
  12. }

In short, we can be found in the package of “org.hibernate.dialect” corresponding to the database dialect class. In which we need to pay attention to little:

a, inherits the parent class constructor, the default constructor call “registerHibernateType (int code, String name)” method to the type of data in the database is mapped to the corresponding Java type. code indicates the type of data in the database integer corresponding database types can be found in the the java.sql.Types class. name that we want to map Java type. Found from “org.hibernate.Hibernate”.

b, Types class. Types defined in the database of commonly used field types, such as:

Java code
  1. ……
  2. public   final   static   int a LONGVARCHAR is = – 1;
  3. public   final   static   int TIMESTAMP = 93;
  4. ……

We can “No Dialect mapping for JDBC type:” followed by the number in the class (Types), find the appropriate type.We can also find the appropriate value based on the type of the field in the data table. This value isregisterHibernateType (int code, String name) the first parameter.

c, Hibernate class. The Hibernate defined the purpose of this conversion type, such as shown in the first piece of code. Can be converted into what type, can find in this class. By calling the getName () method to get a String type.Of course, if you remember, we can write

Java code
  1. import org.hibernate.dialect.SQLServerDialect;
  2. public   class SqlServer2008Dialect extends SQLServerDialect {
  3.      the public SqlServer2008Dialect () {
  4.          super ();
  5. registerHibernateType (1, “string”);
  6. registerHibernateType (- 9, “string”);
  7. registerHibernateType (- 16, “string”);
  8. registerHibernateType (3, “double”);
  9. }
  10. }

In fact, is the same as above, but the above represents the value directly write out. O (? _ ?) O haha ??~. Note thatsuper () method call, whether the method is not called an error, I do not know, I have not done this test, so it is best to adjust.

Then, we also need to make changes in the configuration file. I use EJB3, so I added in the META-INF folder under the persistent.xml file property hibernate.dialect “value value xxx.xxx.SqlServer2008Dialect” xxx said the package name, we all know this. The code is as follows:

Java code
  1. <? Xml version = “1.0” encoding = “UTF-8”?>
  2. <Persistence xmlns =
  3. xmlns: xsi =
  4. xsi: schemaLocation = “
  5. version = “1.0”>
  6. <persistence-unit name= “DateSource”>
  7. <jta-data-source> java :/ SqlServerDS </ jta-data-source>
  8. <properties>
  9. <property name= “hibernate.dialect” value= “xxx.xxx.SqlServer2008Dialect” />
  10. <property name= “hibernate.hbm2ddl.auto” value= “none” />
  11. </ Properties>
  12. </ Persistence-unit>
  13. </ Persistence>

Packaged deployment, OK! Of course, if you’re using is Hibernate, you modify the Hibernate configuration file hibernate.cfg.xml changed to their own dialect class “xxx.xxx.SqlServer2008Dialect” can “hibernate.dialect the value of the property.