2012-06-14 55 views
1

從數據庫中提取行時,出現org.hibernate.MappingException: No Dialect mapping for JDBC type: -1錯誤。org.hibernate.MappingException:否JDBC類型的方言映射:-1

錯誤的詳細信息

exception 

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.MappingException: No Dialect mapping for JDBC type: -1 
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659) 
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
root cause 

org.hibernate.MappingException: No Dialect mapping for JDBC type: -1 
    org.hibernate.dialect.TypeNames.get(TypeNames.java:56) 
    org.hibernate.dialect.TypeNames.get(TypeNames.java:81) 
    org.hibernate.dialect.Dialect.getHibernateTypeName(Dialect.java:370) 
    org.hibernate.loader.custom.CustomLoader$Metadata.getHibernateType(CustomLoader.java:559) 
    org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.performDiscovery(CustomLoader.java:485) 
    org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:501) 
    org.hibernate.loader.Loader.getResultSet(Loader.java:1796) 
    org.hibernate.loader.Loader.doQuery(Loader.java:674) 
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236) 
    org.hibernate.loader.Loader.doList(Loader.java:2213) 
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104) 
    org.hibernate.loader.Loader.list(Loader.java:2099) 
    org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289) 
    org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695) 
    org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142) 
    org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152) 
    com.report.dao.BestSellerReportDAO.getBestSellReport(BestSellerReportDAO.java:82) 
    com.report.service.BestSellerService.getBestSellReport(BestSellerService.java:21) 
    com.report.service.BestSellerService$$FastClassByCGLIB$$3c172a2c.invoke(<generated>) 
    net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149) 
    org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:692) 
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:108) 
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625) 
    com.report.service.BestSellerService$$EnhancerByCGLIB$$1ca7eb9d_2.getBestSellReport(<generated>) 
    com.report.controller.BestSellerReportController.onSubmit(BestSellerReportController.java:55) 
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    java.lang.reflect.Method.invoke(Method.java:597) 
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710) 
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167) 
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414) 
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402) 
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771) 
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716) 
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647) 
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 

使用代碼:

query = sessionFactory.getCurrentSession().createSQLQuery("CALL my_sp_test(:gender_id,:category_id,:is_date_criteria_on,:from_date,:to_date,:unit_sold_above)"); 
query.setParameter("gender_id",bestSellCriteriaForms[0].getSelectedGender());  query.setParameter("category_id",bestSellCriteriaForms[0].getSelectedDepartment()); 
query.setParameter("unit_sold_above", 200); 
query.setParameter("is_date_criteria_on", true); 
query.setParameter("from_date", date); 
query.setParameter("to_date", date); 

//Get query as list 
reportList = query.list(); 

我尋找這個錯誤,但coudn't找到任何解決辦法呢。

我該如何解決這個問題? (老實說,我不明白爲什麼會發生)

+0

什麼是你使用的底層數據庫? – kyiu

+0

@KHY我正在使用mysql –

+0

在您的hibernate.cfg.xml文件中設置MySQLDialect。 –

回答

2

我原來決心我的答案寫我自己的方言類作爲

import java.sql.Types; 

import org.hibernate.Hibernate; 
import org.hibernate.dialect.MySQLDialect; 
import org.hibernate.dialect.function.StandardSQLFunction; 

public class CustomeDialect extends MySQLDialect { 


public CustomeDialect(){ 
    registerFunction("group_concat", new StandardSQLFunction("group_concat", Hibernate.STRING)); 
    registerFunction("coalesce", new StandardSQLFunction("coalesce", Hibernate.STRING)); 
    registerHibernateType(Types.LONGVARCHAR, Hibernate.TEXT.getName()); 

} 
} 

,並添加到disptcher-servlet.xml中的

<bean id="MySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="mydbDatasource"/> 
    <property name="configurationClass"> 
      <value>org.hibernate.cfg.AnnotationConfiguration</value> 
    </property> 
    <property name="packagesToScan" value="com.example.entity" /> 
    <property name="hibernateProperties"> 
     <value> 
     hibernate.dialect=com.example.utils.CustomeDialect 
     </value> 
    </property> 
    </bean> 

及其對我工作的罰款。

+0

謝謝,它也適用於我。 –

1

像這樣創建hibernate.cfg.xml。

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.bytecode.use_reflection_optimizer">false</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.password">password</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mkyong</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="show_sql">true</property> 
    <mapping resource="com/mkyong/common/Stock.hbm.xml"></mapping> 
</session-factory> 

1

的問題是返回類型。什麼是my_sp_test的返回類型? JDBC驅動程序將返回類型報告爲-1,但Hibernate不知道如何解釋該類型,並且您沒有告訴它如何去做。

我的猜測是,my_sp_test沒有返回類型,但你試圖通過選擇查詢設施來使用它。如果my_sp_test沒有返回,那麼你可以嘗試調用Query.executeUpdate() insteasd Query.list()。我不能肯定地說這會起作用,因爲executeUpdate確實意味着處理UPDATE/DELETE查詢。同樣,如果是這種情況,最好的辦法就是下載到JDBC。

相關問題