使用Hibernate+MySql+native SQL的BUG,以及解决办法
本来是mssql+hibernate+native SQL 应用的很和谐
但是到了把mssql换成mysql,就出了错(同样的数据结构和数据)。
查询方法是:
- String sql =
- "select id XXX_ID from t_tab";
- List<Map> list = session.createSQLQuery(sql)
- .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)
- .list();
错误信息:
- org.hibernate.exception.SQLGrammarException: could not execute query
- at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
- at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
- at org.hibernate.loader.Loader.doList(Loader.java:2231)
- at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
- at org.hibernate.loader.Loader.list(Loader.java:2120)
- at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:312)
- at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1722)
- at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
- at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:175)
- at com.exsun.common.dao.BasDaoImpl.findBySql(BasDaoImpl.java:173)
- at com.exsun.arms.service.EmployeesalaryService.getList(EmployeesalaryService.java:32)
- at com.exsun.arms.service.EmployeesalaryService$$FastClassByCGLIB$$2d81000f.invoke(<generated>)
- at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
- at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
- at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
- at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
- at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
- at com.exsun.arms.service.EmployeesalaryService$$EnhancerByCGLIB$$1e3e6d9f.getList(<generated>)
- at com.exsun.arms.action.EmployeesalaryAction.getList(EmployeesalaryAction.java:110)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
- at java.lang.reflect.Method.invoke(Method.java:597)
- at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
- at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
- at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:110)
- at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
- at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
- at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
- at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
- at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
- at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
- at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
- at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
- at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
- at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
- at com.exsun.common.util.MenuFilter.doFilter(MenuFilter.java:103)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
- at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
- at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
- at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
- at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
- at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
- at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
- at java.lang.Thread.run(Thread.java:619)
- Caused by: java.sql.SQLException: Column 'id' not found.
- at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1072)
- at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:986)
- at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:981)
- at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
- at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1144)
- at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5616)
- at org.hibernate.type.StringType.get(StringType.java:41)
- at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:184)
- at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:210)
- at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.extract(CustomLoader.java:497)
- at org.hibernate.loader.custom.CustomLoader$ResultRowProcessor.buildResultRow(CustomLoader.java:443)
- at org.hibernate.loader.custom.CustomLoader.getResultColumnOrRow(CustomLoader.java:340)
- at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:629)
- at org.hibernate.loader.Loader.doQuery(Loader.java:724)
- at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
- at org.hibernate.loader.Loader.doList(Loader.java:2228)
- ... 62 more
看到最后Caused by: java.sql.SQLException:
所以我试试用JDBC连接
- public class TestMySqlJDBC {
- public static void main(String[] args) throws Exception, IllegalAccessException, ClassNotFoundException {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/arms?useUnicode=true&characterEncoding=utf8&","root","");
- Statement stmt=con.createStatement();
- ResultSet rs=stmt.executeQuery("select id XXX_ID,name from t_tab");
- while(rs.next())
- {
- System.out.printf("id:%s___name:%s/n",rs.getString("XXX_ID"),rs.getString("name"));
- }
- rs.close();
- stmt.close();
- con.close();
- }
- }
可以很正常的运行。
后来仔细又看了一下hibernate的执行错误
Caused by: java.sql.SQLException: Column 'id' not found.
是ID 找不到 不是XXX_ID 找不到。这意思貌似hibernate就没按别名alias 来取值
跟踪了一下hibernate
其中在Customer里发现的这一段
- public class ScalarResultColumnProcessor implements ResultColumnProcessor {
- private int position = -1;
- private String alias;
- private Type type;
- public ScalarResultColumnProcessor(int position) {
- this.position = position;
- }
- public ScalarResultColumnProcessor(String alias, Type type) {
- this.alias = alias;
- this.type = type;
- }
- //这个方法中的alias 为"id"而不是XXX_ID
- public Object extract(
- Object[] data,
- ResultSet resultSet,
- SessionImplementor session) throws SQLException, HibernateException {
- return type.nullSafeGet( resultSet, alias, session, null );
- }
- ...........
- ...........
- ...........
hibernate是按照select id,name from tab来解释
而不是 select id XXX_ID , name from tab
这里跟到的数据果然是Hibernate把alias给忽略了。。。
-------------------------------------
继续跟踪hibernate代码.根据调用栈,单步跟踪很快找到
org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor 里面的
- public void performDiscovery(Metadata metadata, List types, List aliases) throws SQLException {
- if ( alias == null ) {
- alias = metadata.getColumnName( position );
- }
- else if ( position < 0 ) {
- position = metadata.resolveColumnPosition( alias );
- }
- if ( type == null ) {
- type = metadata.getHibernateType( position );
- }
- types.add( type );
- aliases.add( alias );
- }
这里就是拼装将来要根据列名get出来的结果的地方
alias = metadata.getColumnName( position )
getColumnName 跟进去就是JDBC的实现.
跟踪可以看到.从这里get出来的alias不是真正的aliasName,而是originalColumnName原始列名.
后来试着修改了hibernate各种方言的设置:MySQLDialect,MySQL5Dialect,MySQL5InnoDBDialect等等还是不行。
我用JDBC试了下
mysql-connector-java-5.1.9-bin.jar
mysql-connector-java-5.1.10-bin.jar
mysql-connector-java-5.1.11-bin.jar
- ResultSet rs=stmt.executeQuery(sql);
- ResultSetMetaData rsmd = rs.getMetaData();
- int columnCount = rsmd.getColumnCount();
- List<String> columnName = new ArrayList();
- for (int i = 1; i <= columnCount; i++) {
- String tmp = rsmd.getColumnName(i) + "___" + rsmd.getColumnLabel(i);
- System.out.println(tmp);
- }
三个实现,结果都是一样的.
mysql的JDBC要获取alias只能用getColumnLable,不能用getColumnName
而Hibernate取字段名称的时候就只用 getColumnName.
解决办法有两个,一个是改hibernate,再不就得改mysql.
怕Hibernate对别的数据库实现有影响
所以就拿mysql的JDBC驱动开刀了.
把com.mysql.jdbc.ResultSetMetaData
中的
- public String getColumnName(int column) throws SQLException {
- if (this.useOldAliasBehavior) {
- return getField(column).getName();
- }
- String name = getField(column).getNameNoAliases();
- if (name != null && name.length() == 0) {
- return getField(column).getName();
- }
- return name;
- }
修改为:
- public String getColumnName(int column) throws SQLException {
- return getColumnLable(column);
- }
然后把JDBC重新打包一下
再运行就OK了.
使用Hibernate+MySql+native SQL的BUG,以及解决办法的更多相关文章
- 13.hibernate的native sql查询(转自xiaoluo501395377)
hibernate的native sql查询 在我们的hibernate中,除了我们常用的HQL查询以外,还非常好的支持了原生的SQL查询,那么我们既然使用了hibernate,为什么不都采用hi ...
- Mysql文件太大导入失败解决办法总结
Mysql文件太大导入失败解决办法总结 在使用phpmyadmin导入数据库的时候可能会碰到由于数据库文件太大而无法导入的问题! 英文提示如下:File exceeds the maximum all ...
- Dumpzilla工具第615行bug的解决办法
Dumpzilla工具第615行bug的解决办法 在Dumpzilla使用选项frequency时,会提示SQL语法错误.这是由于其中SQL语句编写错误.需要将615行中: where url l ...
- MySQL8.0报错Can't connect to MySQL server on 'localhost' (10061)的解决办法
MySQL8.0报错Can't connect to MySQL server on 'localhost' (10061)的解决办法 事情的起因 今天课堂上要展示小组项目,需要用一个软件叫W ...
- 【转载】IE浏览器常见的9个css Bug以及解决办法
IE浏览器常见的9个css Bug以及解决办法 我们在浏览网页的时候经常看见这样的现象:某个网页在IE6浏览器中打开很正常,但是在IE8里面打开可能完全变形了.或者也有可能出现完全相反的现象.这让We ...
- 无法启动MYSQL服务”1067 进程意外终止”解决办法
原文:http://www.111cn.net/database/mysql/48888.htm 本文章主要是总结了各种导致mysql提示无法启动MYSQL服务"1067 进程意外终止& ...
- 无法启动mysql服务”1067 进程意外终止”解决办法【简记】
本文章主要是总结了各种导致mysql提示无法启动MYSQL服务”1067 进程意外终止”的一些解决办法,有碰到mysql无法启动的同学可尝试参考. 在win7的服务器里开启MySql服务提示“wind ...
- mysql的大量的sleep进程解决办法
mysql的大量的sleep进程解决办法 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/liuyong0507/article/detai ...
- windows下运行命令行mysql,提示mysql不是内部命令,解决办法
1. 打开CMD命令行,连接本地mysql数据库:mysql -u root -p 提示‘mysql’不是本地命令,解决办法如下: 找到你安装的mysql的目录,打开当前目录下的bin文件夹,你可以看 ...
随机推荐
- 解决PLSQL 查询后显示中文为问号(???)问题
我的问题已解决,在装oracle的服务器上配置了下面的两个环境变量后,重启服务器,重新录入中文,在查询即可正确显示中文. 原因: 本机(装oracle的服务器)没有配置数据库字符集环境变量,或是与数据 ...
- centos 默认php 版本太低移到高版本的办法
// centos 默认有php 版本太低转移到高版本的解决办法 php -v 版本低 ln -s /usr/local/php/bin/php /usr/bin/php
- php基础 php 全局变量
$_GET ----->get传送方式 $_POST ----->post传送方式 $_REQUEST ----->可以接收到get和post两种方式的值 $GLOBALS ---- ...
- PAT T1008 Airline Routes
用tarjan算法缩点~ #include<bits/stdc++.h> using namespace std; ; vector<int> g[maxn]; int N,M ...
- C. Swap Letters 01字符串最少交换几次相等
C. Swap Letters time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- 聚合数据实名认证接口-java方法
只需要填入购买的APPKEY,然后直接调用方法JuheDemo.info(user_name, anchor_card);传入姓名和身份证号,根据获取的返回参数进行拆分,如res=1说明正确. //进 ...
- js中的||和&&的用法
与其他语言不同,在JS中,a&&b或者a||b返回的是要么是a,要么是b:而其他语言中返回的是true or false 对于js中的或与运算,需要隐式的转换为boolean类型再来运 ...
- ABC154F - Many Many Paths
梦回高中,定义的f(i,j)为从(0,0)到(i,j)一共有多少条路可以选择,易知我们要做i+j次选择,其中有i次是选择x轴,剩下的是y轴,所以f(i,j)=C(i+j,i)=C(i+j,j),给你一 ...
- mysql 通过localhost可以连接IP连接不上
因为MySQL默认没开 所以需要手动设置 开MySQL启远程连接的功能,在MySQL服务器控制台上执行以下命令: 设置权限: grant all privileges on *.* to 用户名@&q ...
- 大部分政府网站U-mail存在直接拿shell漏洞
大部分网站政府网站U-mail存在直接拿shell漏洞加入webmail/userapply.php?execadd=333&DomainID=111直接爆出物理地址 然后将 aa' unio ...