Hibernate sqlquery MySQL alias problem

Source: Internet
Author: User
Tags aop connection pooling



Project Environment: hibernate3.6.1 mysql5.7.3 Drive: mysql-connector-java-5.1.26

Problem Description: The project was flipped from a SQL Server database onto the MySQL database. The SQL query is automatically mapped to the object, but always an error. Strange problem: SQL print out on database client

Results can be executed normally. But SQL is the query does not come out, the report Column ' account ' is not found. Such a mistake. Change the drive, think about the framework of the call method, when querying data, found that someone

Say with Query.addentity ("", ""). List () to resolve the problem and not verify. Because you don't want to take great pains to change from the frame. With the driver source tracking, found that the original driver does not recognize the alias. In the query

' UserAccount ' corresponds to the value of the time, to the driver ColumnName unexpectedly is "account", which is also a lot of people on the internet said, if Sqlquey encountered two tables have the same property name, the query will

The cause of the bug is the error!

Code section:

        SQLQuery query = Createsqlquery (Ordersql, values); Long totalcount = Countsqlresult (sql, values);p Age.settotalitems ( TotalCount); setpageparametertosqlquery (query, page); List result = Query.setresulttransformer (Transformers.aliastobean (CL)). List ();p age.setresult (result);

Platform output:

Select        l.id,        userId,        u.account as ' useraccount ',        IP,        ipaddr,        logintime     from        t_ Loginlog L left     joins        t_user u on             l.userid = u.id     where        1=1 limit?


[2015-01-30 14:27:29,031] [ERROR] [Http-8080-1] (jdbcexceptionreporter.java:234)-Column ' account ' is not found.2015-1-30 14:27:29 Org.apache.catalina.core.StandardWrapperValve Invoke severity: Servlet.service () for Servlet Ishare threw Exceptionjava.sql.SQLException:Column ' account ' is not found.at com.mysql.jdbc.SQLError.createSQLException ( sqlerror.java:1078) at Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:989) at Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:975) at Com.mysql.jdbc.SQLError.createSQLException ( sqlerror.java:920) at Com.mysql.jdbc.ResultSetImpl.findColumn (resultsetimpl.java:1171) at Com.mysql.jdbc.ResultSetImpl.getString (resultsetimpl.java:5737) at Org.apache.commons.dbcp.DelegatingResultSet.getString (delegatingresultset.java:263) at Org.apache.commons.dbcp.DelegatingResultSet.getString (delegatingresultset.java:263) at Org.hibernate.type.descriptor.sql.varchartypedescriptor$2.doextract (varchartypedescriptor.java:61) at Org.hibernate.type.descriptor.sql.BasicExtractor.Extract (basicextractor.java:64) at Org.hibernate.type.AbstractStandardBasicType.nullSafeGet ( abstractstandardbasictype.java:253) at Org.hibernate.type.AbstractStandardBasicType.nullSafeGet ( abstractstandardbasictype.java:249) at Org.hibernate.type.AbstractStandardBasicType.nullSafeGet ( abstractstandardbasictype.java:234) at org.hibernate.loader.custom.customloader$ Scalarresultcolumnprocessor.extract (customloader.java:505) at org.hibernate.loader.custom.customloader$ Resultrowprocessor.buildresultrow (customloader.java:451) at Org.hibernate.loader.custom.CustomLoader.getResultColumnOrRow (customloader.java:348) at Org.hibernate.loader.Loader.getRowFromResultSet (loader.java:639) at Org.hibernate.loader.Loader.doQuery ( loader.java:829) at Org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections (loader.java:274) at Org.hibernate.loader.Loader.doList (loader.java:2533) at Org.hibernate.loader.Loader.listIgnoreQueryCache ( loader.java:2276) at Org.hibernate.loader.Loader.list (loader.java:2271) at Org.hibErnate.loader.custom.CustomLoader.list (customloader.java:316) at Org.hibernate.impl.SessionImpl.listCustomQuery ( sessionimpl.java:1842) at Org.hibernate.impl.AbstractSessionImpl.list (abstractsessionimpl.java:165) at Org.hibernate.impl.SQLQueryImpl.list (sqlqueryimpl.java:157) at Com.coolsoul.framework.orm.hibernate.HibernateDao.findSqlPage (hibernatedao.java:90) at Com.coolsoul.ishare.loginLog.service.LoginLogService.findPageList (loginlogservice.java:54) at com.coolsoul.ishare.loginlog.service.loginlogservice$ $FastClassByCGLIB $ $fb 9ca761.invoke (<generated>) at Net.sf.cglib.proxy.MethodProxy.invoke (methodproxy.java:191) at org.springframework.aop.framework.cglib2aopproxy$ Cglibmethodinvocation.invokejoinpoint (cglib2aopproxy.java:688) at Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:150) at Org.springframework.transaction.interceptor.TransactionInterceptor.invoke (transactioninterceptor.java:110) at Org.springframework.aop.framework.ReflectiveMEthodinvocation.proceed (reflectivemethodinvocation.java:172) at Org.springframework.aop.framework.Cglib2AopProxy $DynamicAdvisedInterceptor. Intercept (cglib2aopproxy.java:621) at com.coolsoul.ishare.loginlog.service.loginlogservice$ $EnhancerByCGLIB $$5ea6d36b.findpagelist (<generated> ) at Com.coolsoul.ishare.loginLog.controller.LoginLogController.queryList (loginlogcontroller.java:69) 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.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod ( handlermethodinvoker.java:176) at Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod ( annotationmethodhandleradapter.java:426) at Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlEradapter.handle (annotationmethodhandleradapter.java:414) at Org.springframework.web.servlet.DispatcherServlet.doDispatch (dispatcherservlet.java:790) at Org.springframework.web.servlet.DispatcherServlet.doService (dispatcherservlet.java:719) at Org.springframework.web.servlet.FrameworkServlet.processRequest (frameworkservlet.java:644) at Org.springframework.web.servlet.FrameworkServlet.doPost (frameworkservlet.java:560) at Javax.servlet.http.HttpServlet.service (httpservlet.java:710) at Javax.servlet.http.HttpServlet.service ( httpservlet.java:803) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter ( applicationfilterchain.java:290) at Org.apache.catalina.core.ApplicationFilterChain.doFilter ( applicationfilterchain.java:206) 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:235) at Org.apache.catalina.core.ApplicationFilterChain.doFilter ( applicationfilterchain.java:206) at Org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal ( characterencodingfilter.java:88) at Org.springframework.web.filter.OncePerRequestFilter.doFilter ( onceperrequestfilter.java:76) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter ( applicationfilterchain.java:235) at Org.apache.catalina.core.ApplicationFilterChain.doFilter ( applicationfilterchain.java:206) at Org.apache.catalina.core.StandardWrapperValve.invoke ( standardwrappervalve.java:230) at Org.apache.catalina.core.StandardContextValve.invoke (Standardcontextvalve.java : 175) at Org.apache.catalina.core.StandardHostValve.invoke (standardhostvalve.java:128) at Org.apache.catalina.valves.ErrorReportValve.invoke (errorreportvalve.java:104) at Org.apache.catalina.core.StandardEngineValve.invoke (standardenginevalve.java:109) at Org.apache.catalina.connector.CoyoteAdapter.servicE (coyoteadapter.java:261) at org.apache.coyote.http11.Http11Processor.process (http11processor.java:844) at Org.apache.coyote.http11.http11protocol$http11connectionhandler.process (http11protocol.java:581) at Org.apache.tomcat.util.net.jioendpoint$worker.run (jioendpoint.java:447) at Java.lang.Thread.run (thread.java:662)


How to Save:

Troubled me for a long time, find a lot of information, the problem is very simple, is the driver does not support the problem of aliases, recorded here, for the Yimeimei build Bridge!!! When connecting to MySQL database, add useoldaliasmetadatabehavior=true this parameter!!!!


jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/db?useunicode\=true&characterencoding\=utf8& Useoldaliasmetadatabehavior=true

Some of the more important parameters are commonly used:

Parameter name Parameter description Default value Minimum version requirements
User Database user name (used to connect to database) All versions
Password User password (used to connect to database) All versions
Useunicode Whether to use the Unicode character set if the parameter characterencoding is set to gb2312 or GBK False 1.1g
Characterencoding When Useunicode is set to True, the character encoding is specified. For example, can be set to gb2312 or GBK False 1.1g
AutoReConnect Whether to automatically reconnect when the database connection is interrupted abnormally False 1.1
Autoreconnectforpools Whether to use a reconnection policy for database connection pooling False 3.1.3
Failoverreadonly Whether the connection is set to read-only after automatic reconnection is successful True 3.0.12
Maxreconnects When AutoReConnect is set to true, the number of times to retry the connection 3 1.1
Initialtimeout When AutoReConnect is set to true, the time interval between two re-interconnects, in seconds 2 1.1
ConnectTimeout Timeout when establishing a socket connection to the database server, in milliseconds, 0 for JDK 1.4 and later 0 3.0.1
Sockettimeout Socket operation (Read-write) timeout, in milliseconds. 0 means never time out 0 3.0.1




Hibernate sqlquery MySQL alias problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.