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:
- import java.sql.Types;
- import org.hibernate.Hibernate;
- import org.hibernate.dialect.SQLServerDialect;
- public class SqlServer2008Dialect extends SQLServerDialect {
- the public SqlServer2008Dialect () {
- super ();
- registerHibernateType (Types.CHAR, Hibernate.STRING.getName ());
- registerHibernateType (Types.NVARCHAR, Hibernate.STRING.getName ());
- registerHibernateType (Types.LONGNVARCHAR, Hibernate.STRING.getName ());
- registerHibernateType (Types.DECIMAL, Hibernate.DOUBLE.getName ());
- }
- }
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:
- ……
- public final static int a LONGVARCHAR is = – 1;
- public final static int TIMESTAMP = 93;
- ……
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
- import org.hibernate.dialect.SQLServerDialect;
- public class SqlServer2008Dialect extends SQLServerDialect {
- the public SqlServer2008Dialect () {
- super ();
- registerHibernateType (1, “string”);
- registerHibernateType (- 9, “string”);
- registerHibernateType (- 16, “string”);
- registerHibernateType (3, “double”);
- }
- }
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:
- <? Xml version = “1.0” encoding = “UTF-8”?>
- <Persistence xmlns = “
- xmlns: xsi = “
- schemaLocation = “
- version = “1.0”>
- <persistence-unit name= “DateSource”>
- <jta-data-source> java SqlServerDS </ jta-data-source>
- <properties>
- <property name= “hibernate.dialect” value= “xxx.xxx.SqlServer2008Dialect” />
- <property name= “hibernate.hbm2ddl.auto” value= “none” />
- </ Properties>
- </ Persistence-unit>
- </ 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.