Hibernate Custom dialect

Source: Internet
Author: User

 Customizing a dialect class--hibernate dialectTags: hibernatesqlserverjdbcmysql database java2012-07-04 18:46 2847 people read Comments (1) favorite reports Classification:Hibernate (+)

The class needs to inherit the dialect class corresponding to the database we are using. For example: If we are using MySQL (version 5.x.x), we need to inherit "Org.hibernate". dialect. Mysql5dialect "; If we are using DB2, then we should inherit" Org.hibernate.dialect.DB2Dialect "; I use SqlServer2008, so I will inherit" Org.hibernate.dialect.SQLServerDialect "

[Java]View Plaincopy
  1. Import Java.sql.Types;
  2. Import Org.hibernate.Hibernate;
  3. Import Org.hibernate.dialect.MySQL5Dialect;
  4. /**
  5. * Rewrite Mysql5dialect class, register types
  6. * @author LZ
  7. *
  8. */
  9. Public class Mydialect extends Mysql5dialect {
  10. Public Mydialect () {
  11. super ();
  12. Registerhibernatetype (Types.decimal, Hibernate.BIG_DECIMAL.getName ());
  13. Registerhibernatetype (Types.longvarchar,hibernate.string.getname ());
  14. Registerhibernatetype (Types.binary,hibernate.string.getname ());
  15. Registerhibernatetype (-1, Hibernate.STRING.getName ());
  16. }
  17. }


Note: If your database is MySQL and the decimal type is used, the error should be No dialect mapping for JDBC type:3. Note This 3, which indicates 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 the decimal type is used, the error should be No dialect mapping for JDBC type:3. Note This 3, which indicates that hibernate cannot map this data type to your Java class. It needs to be used in custom dialects:

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, it is necessary to include in the dialect:

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

[HTML]View Plaincopy
    1. <property name="Hibernate.dialect">
    2. Com.yourcompany.MyDialect
    3. </Property>

Hibernate Custom dialect

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.