org.hibernate.MappingException: No Dialect mapping for JDBC type: -1 解决方法

错误代码:

[c-sharp] view plain copy
  1. org.hibernate.MappingException: No Dialect mapping for JDBC type: -1  
  2.     at org.hibernate.dialect.TypeNames.get(TypeNames.java:79)  
  3.     at org.hibernate.dialect.TypeNames.get(TypeNames.java:104)  
  4.     at org.hibernate.dialect.Dialect.getHibernateTypeName(Dialect.java:393)  
  5.     at org.hibernate.loader.custom.CustomLoader$Metadata.getHibernateType(CustomLoader.java:582)  
  6.     at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.performDiscovery(CustomLoader.java:508)  
  7.     at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:524)  
  8.     at org.hibernate.loader.Loader.getResultSet(Loader.java:1821)  
  9.     at org.hibernate.loader.Loader.doQuery(Loader.java:697)  
  10.     at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)  
  11.     at org.hibernate.loader.Loader.doList(Loader.java:2232)  
  12.     at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)  
  13.     at org.hibernate.loader.Loader.list(Loader.java:2124)  
  14.     at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:312)  
  15.     at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1723)  
  16.     at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)  
  17.     at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:175)  
  18.     at com.lawyer.dao.ILawFirmDaoImpl.getHeZuoLawFirms(ILawFirmDaoImpl.java:143)  
  19.     at com.lawyer.service.ILawFirmServiceImpl.getHeZuoLawFirms(ILawFirmServiceImpl.java:40)  
  20.     at com.lawyer.action.IndexAction.loadHostLawFirm(IndexAction.java:110)  
  21.     at com.lawyer.action.IndexAction.execute(IndexAction.java:54)  
  22.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  23.     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)  
  24.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)  
  25.     at java.lang.reflect.Method.invoke(Method.java:597)  
  26.     at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)  
  27.     at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)  
  28.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)  
  29.     at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)  
  30.     at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)  
  31.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  32.     at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)  
  33.     at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)  
  34.     at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)  
  35.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  36.     at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)  
  37.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  38.     at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)  
  39.     at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)  
  40.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  41.     at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)  
  42.     at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)  
  43.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  44.     at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)  
  45.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  46.     at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)  
  47.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  48.     at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)  
  49.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  50.     at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)  
  51.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  52.     at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)  
  53.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  54.     at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)  
  55.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  56.     at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)  
  57.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  58.     at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)  
  59.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  60.     at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)  
  61.     at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)  
  62.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  63.     at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)  
  64.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  65.     at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)  
  66.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  67.     at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)  
  68.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  69.     at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)  
  70.     at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)  
  71.     at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)  
  72.     at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)  
  73.     at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)  
  74.     at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)  
  75.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)  
  76.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)  
  77.     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)  
  78.     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)  
  79.     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)  
  80.     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)  
  81.     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)  
  82.     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)  
  83.     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)  
  84.     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)  
  85.     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)  
  86.     at java.lang.Thread.run(Thread.java:619)  

 

解决方法:1

[c-sharp] view plaincopy
  1. org.hibernate.MappingException: No Dialect mapping for JDBC type: -1异常是因为hibernate不支持数据库中的text类型,就是说hibernate没有注册该类型,而我们往往要用到这个类型  
  2.   
  3. 解决办法:最简单的是用hql文,不用sql  
  4.   
  5. 但有些时候不得不用sql,网上很多修改配置文件或者自己创建类继承等的方法,但个人觉得简单好用的办法是在取数据的时候用CONVERT函数,将text转成varchar,简单方便  
  6.   
  7. 例如:CONVERT(varchar(100), isnull(bz, ''))  
  8.   
  9. bz是text类型变量名,isnull函数判断下是否为空  
  10.    

解决方法:2

[c-sharp] view plaincopy
  1. 问题原因:数据库表中有text类型的字段,而Hibernate在native查询中没有注册这个字段,因此发生这个错误。   
  2. 解决方法:写一个类、修改hibernate配置文件。 写一个Dialect的子类,这里我 extends MySQL5Dialect类:   
  3. package xxx.xxx;    //xxx.xxx自己根据情况来写 import java.sql.Types;   
  4. import org.hibernate.dialect.MySQL5Dialect;   
  5. public class DialectForInkfish extends MySQL5Dialect {   
  6.     public DialectForInkfish() {   
  7.         super();   
  8.         registerHibernateType(Types.LONGVARCHAR, 65535, "text");   
  9.     }   
  10. }   
  11. 修改Hibernate配置文件hibernate.cfg.xml,把  
  12. "dialect">org.hibernate.dialect.MySQL5Dialect   
  13. 修改为:  
  14. "dialect">com.ibm.crl.inkfish.config.DialectForInkfish 

你可能感兴趣的:(org.hibernate.MappingException: No Dialect mapping for JDBC type: -1 解决方法)