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
- 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 ();
- Registerhibernatetype (Types.decimal, Hibernate.BIG_DECIMAL.getName ());
- Registerhibernatetype (Types.longvarchar,hibernate.string.getname ());
- Registerhibernatetype (Types.binary,hibernate.string.getname ());
- Registerhibernatetype (-1, Hibernate.STRING.getName ());
- }
- }
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
- <property name="Hibernate.dialect">
- Com.yourcompany.MyDialect
- </Property>
Hibernate Custom dialect