Today, we made a query that uses hibernate to directly execute native SQL. The error "No dialect mapping for JDBC Type:-9" is returned.
I checked it online. There are many solutions. Here is a summary. For your convenience.
First: No dialect mapping for JDBC Type: 3;
Error resolution record-No dialect mapping for JDBC Type: 3;
Today, we are using getsession (). createsqlquery ("select sum (aloneip) from table"). List (); Error: No dialect mapping for JDBC Type: 3;Baidu, after Google, everyone said: "The reason is that the data type on the server cannot be mapped to the bigdecimal data type of Java ."; However, the type of the "aloneip" column in my database is integer. I do not know why this error occurs. Follow these steps to resolve the problem: 1. Create a New mmysqldialect extends org. hibernate. dialect. mysqldialect (my database is MySQL) and register a new type ing. Import java. SQL. types; Import org. hibernate. hibernate; Import org. hibernate. dialect. mysqldialect; Public class mmysqldialect extends mysqldialect { Public mmysqldialect (){ Super (); Registerhibernatetype (types. decimal, hibernate. big_integer.getname ()); } }
2. Change Dialect in hibernate to the New Dialect created in step 1. <Prop key = "hibernate. dialect"> [Package path]. mmysqldialect </prop> |
Type 2: org. hibernate. mappingexception: No dialect mapping for JDBC Type:-1
Environment: SQL server2005 + hibernate3.2.5
Cause:
The database table contains text fields, but hibernate does not register this field in native queries. Therefore, this error occurs.
Solution:
Write a class and modify the hibernate configuration file. Write a subclass of dialect. Here I use the extends sqlserverdialectt class:
Package com. ZY. util;
Import java. SQL. types;
Import org. hibernate. hibernate;
Import org. hibernate. dialect. sqlserverdialect;
Public class dialectforinkfish extends sqlserverdialect {
Public dialectforinkfish (){
Super ();
// Registerhibernatetype (types. longvarchar, 65535, "text"); //. longvarchar
Registerhibernatetype (types. decimal, hibernate. big_decimal.getname ());
Registerhibernatetype (-1, hibernate. String. getname ());
}
}
Modify the hibernate configuration file hibernate. cfg. XML
<Property name = "dialect">
Org. hibernate. dialect. sqlserverdialect
</Property>
Modify:
<Property name = "dialect">
Com. ZY. util. dialectforinkfish
</Property>
NOTE: If your database is MySQL and the decimal type is used, the error should be no dialect mapping for JDBC Type: 3. note that this 3 indicates that hibernate cannot map this data type to your Java class. it must be used in a custom Dialect:
Registerhibernatetype (types. decimal, hibernate. big_decimal.getname ());
A more detailed description:
Some time ago, I encountered this type of error. The last question mark indicates an uncertain number, but the solution is the same.
First, define a dialect class-hibernate dialect, which must inherit the dialect class corresponding to the database we use. For example, if we use MySQL (version 5. x. x), we need to inherit from "Org. hibernate. dialect. mysql5dialect "; if we use DB2, we should inherit" Org. hibernate. dialect. db2dialect "; I use sqlserver2008, so I want to inherit" Org. hibernate. dialect. sqlserverdialect ", the reference code is as follows:
Java code
- ImportJava. SQL. types;
- ImportOrg. hibernate. hibernate;
- ImportOrg. hibernate. dialect. sqlserverdialect;
- Public ClassSqlserver2008dialectExtendsSqlserverdialect {
- PublicSqlserver2008dialect (){
- 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 ());
- }
- }
import java.sql.Types;import org.hibernate.Hibernate;import org.hibernate.dialect.SQLServerDialect;public class SqlServer2008Dialect extends SQLServerDialect {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, you can find the Dialect Corresponding to the database in the package org. hibernate. dialect. Note the following three points:
A. inherit the parent class constructor from the default constructor and call the "registerhibernatetype (INT code, string name)" method to map the data type in the database to the corresponding Java type. Code indicates the integer expression of the Data Type in the database. You can find the corresponding database type in the "Java. SQL. types" class. Name indicates the Java type to be mapped. You can find it in "org. hibernate. hibernate.
B. types class. Common Field Types in the database are defined in types, such:
Java code
- ......
- Public Final Static IntLongvarchar =-1;
- Public Final Static IntTimestamp = 93;
- ......
……public final static int LONGVARCHAR = -1;public final static int TIMESTAMP = 93;……
We can find the corresponding type in the class (types) according to the number followed by "No dialect mapping for JDBC Type. We can also find the corresponding value based on the field type in the data table. This value is the first parameter of registerhibernatetype (INT code, string name.
C. hibernate class. Hibernate defines the type of conversion, as shown in the first code. Which type can be converted to? You can search for it in this class. Call the "getname ()" method to obtain a string type. Of course, if you remember it, we can write it like this.
Java code
- ImportOrg. hibernate. dialect. sqlserverdialect;
- Public ClassSqlserver2008dialectExtendsSqlserverdialect {
- PublicSqlserver2008dialect (){
- Super();
- Registerhibernatetype (1, "string ");
- Registerhibernatetype (-9, "string ");
- Registerhibernatetype (-16, "string ");
- Registerhibernatetype (3, "double ");
- }
- }
import org.hibernate.dialect.SQLServerDialect;public class SqlServer2008Dialect extends SQLServerDialect {public SqlServer2008Dialect() {super();registerHibernateType(1, "string");registerHibernateType(-9, "string");registerHibernateType(-16, "string");registerHibernateType(3, "double");}}
In fact, it is the same as above. It just writes out the values represented above. O (worker _ worker) O Haha ~. It should be noted that the super () method is called. I don't know if an error will occur if I don't call this method. I have not tested this, so it is best to call it.
Then, we also need to modify it in the configuration file. I'm using ejb3, so I'm in persistent under the META-INF folder. add the property "hibernate. dialect ", and the value is" XXX. xxx. sqlserver2008dialect ", xxx indicates the package name, which everyone knows. The Code is as follows:
Java code
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Persistence xmlns = "http://java.sun.com/xml/ns/persistence"
- Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
- Xsi: schemalocation = "http://java.sun.com/xml/ns/persistence
- Http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
- 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>
<?xml version="1.0" encoding="UTF-8" ?><persistence xmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"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>
Package and deploy, OK! Of course, if you are using hibernate, modify the hibernate configuration file. cfg. XML, "hibernate. the dialect attribute value is changed to its own dialect class "XXX. xxx. sqlserver2008dialect.
What I encountered was an error 9:
But I didn't use the above method.
I just want to specify the type of the returned value.
Public list topcatalist (INT catalevel ){
Session session = getsession ();
List list = new arraylist ();
String SQL = "select tushu_daima from magazine_cata where cata_level =: Level ";
Try {
Query query = session. createsqlquery (SQL). addscalar ("tushu_daima", hibernate. String );
Query. setinteger ("level", catalevel );
List = query. List ();
} Catch (hibernateexception e ){
E. printstacktrace ();
}
Return list;
}
Pay attention to the red part.