自定义Hibernate Dialect解决createSQLQuery时的decimal,long类型问题
2007-07-16 14:32

近日做了个项目,数据持久层用的是hibernate.第一次用它就发现这是一个很好的东西.但是难免会碰到问题.

问题:

因项目需要,我要做一个通用的查询页面,这里用到一个SQL模板,开始我试了一下,成功.但是后来真正想查询的时候,问题出现了!错误信息如下:

org.hibernate.MappingException: No Dialect mapping for JDBC type: 3
at org.hibernate.dialect.TypeNames.get(TypeNames.java:56)
at org.hibernate.dialect.TypeNames.get(TypeNames.java:81)
at org.hibernate.dialect.Dialect.getHibernateTypeName(Dialect.java:192)
at org.hibernate.loader.custom.CustomLoader.getHibernateType(CustomLoader.java:170)
at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:138)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1678)
at org.hibernate.loader.Loader.doQuery(Loader.java:662)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2150)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:117)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1607)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:121)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:169)
at com.yonder.pm.query.generalQuery.QueryResultViewModel.initDataset(QueryResultViewModel.java:36)
at com.yonder.pm.query.generalQuery.QueryResultViewModel.initDatasets(QueryResultViewModel.java:26)
at com.bstek.dorado.view.ViewModelSupport.init(Unknown Source)
at com.bstek.dorado.view.taglib.AbstractViewTag.doStartTag(Unknown Source)
at org.apache.jsp.query.generalQuery.queryResult_jsp._jspx_meth_d_View_0(queryResult_jsp.java:94)
at org.apache.jsp.query.generalQuery.queryResult_jsp._jspService(queryResult_jsp.java:68)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jbpm.webapp.filter.AuthenticationFilter.doFilter(AuthenticationFilter.java:56)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jbpm.web.JbpmContextFilter.doFilter(JbpmContextFilter.java:83)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.bstek.dorado.core.FilterHandle.c(Unknown Source)
at com.bstek.dorado.core.FilterHandle.doFilter(Unknown Source)
at com.bstek.dorado.core.DoradoFilter.doFilter(Unknown Source)
at com.yonder.pm.common.PmFilter.doFilter(PmFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jbpm.webapp.filter.LogFilter.doFilter(LogFilter.java:59)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)

仔细查看,发现问题在于数据类型.到网上查,发现hibernate在执行List result = session.createSQLQuery(sql).list()的时候,当SQL语句中遇到的decimal,long等类型的字段时,就出现上面的错误.而且从错误信息中可以发现:出错的是Dialect.

解决办法:

错误知道以后,我就到网上找解决办法.看来遇到这类问题的人太多了,网上到处都有人贴这个问题.我看了几篇,发现有个解决办法,就是自定义Hibernate Dialect.虽然所用数据库不同(我用的数据库是DB2),我觉得大同小异,就照着做了:

首先建一个类,继承org.hibernate.dialect.DB2Dialect,该类的内容如下:

import java.sql.Types;

import org.hibernate.Hibernate;
import org.hibernate.dialect.DB2Dialect;

public class PmDb2Dialect extends DB2Dialect
{
public PmDb2Dialect()
{
     super();
     registerHibernateType(Types.DECIMAL, Hibernate.BIG_DECIMAL.getName());
}
}

第二步,就是修改hibernate的配置文件hibernate.cfg.xml:

将:

     <property name="hibernate.dialect">
      org.hibernate.dialect.DB2Dialect
     </property>

改为:

     <property name="hibernate.dialect">
      com.yonder.pm.common.PmDb2Dialect
     </property>

posted on 2007-12-01 11:15  无会  阅读(2845)  评论(0编辑  收藏  举报