Customize a dialect class--hibernate dialect

Source: Internet
Author: User

This class needs to inherit the dialect class corresponding to the database we are using. For example: if we use MySQL (version 5.x.x), we need to inherit "org.hibernate.dialect.MySQL5Dialect"; if we use DB2, then we should inherit " Org.hibernate.dialect.DB2Dialect "; I'm using SqlServer2008, so I'm going to inherit" Org.hibernate.dialect.SQLServerDialect "

Import Java.sql.Types;
Import Org.hibernate.Hibernate;
Import Org.hibernate.dialect.MySQL5Dialect;
  /** * Rewrite Mysql5dialect class, register types * @author LZ * * */public class Mydialect extends Mysql5dialect {public mydialect () {
Super ();
Hibernate4 after configuration Registerhibernatetype (Types.decimal, BigDecimalType.INSTANCE.getName ());
Registerhibernatetype (Types.longvarchar,stringtype.instance.getname ());
Registerhibernatetype (Types.binary,stringtype.instance.getname ());

Registerhibernatetype ( -1, StringType.INSTANCE.getName ());
Hibernate3 before the configuration Registerhibernatetype (Types.decimal, Hibernate.BIG_DECIMAL.getName ());
Registerhibernatetype (Types.longvarchar,hibernate.string.getname ());
Registerhibernatetype (Types.binary,hibernate.string.getname ());
Registerhibernatetype ( -1, Hibernate.STRING.getName ());
After the Hibernate3 configuration Registerhibernatetype (Types.decimal, New Bigdecimaltype (). GetName ());
Registerhibernatetype (Types.longvarchar,new StringType (). GetName ()); Registerhibernatetype (Types.binary,new StrinGtype (). GetName ());
 Registerhibernatetype ( -1, New StringType (). GetName ());
 }
}


Description: If your database is MySQL, and use the decimal type, the error should be No dialect mapping for JDBC type:3. Note This 3, which shows that hibernate cannot map this data type to your Java class. It needs to be used in a custom dialect: Description:

If your database is MySQL and uses the decimal type, the error should be No dialect mapping for JDBC type:3. Note This 3, which shows that hibernate cannot map this data type to your Java class. You need to use the following in a custom dialect:

Registerhibernatetype (Types.decimal, Hibernate.BIG_DECIMAL.getName ());

If you use the text data type, hibernate does not know this data type at all, so it returns no dialect mapping for JDBC type:-1

In this case, you need to add in the dialect:

Registerhibernatetype ( -1,hibernate.string.getname ());

<property name= "Hibernate.dialect" >  
com.yourcompany.MyDialect
</property> 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.