mybatis / mybatis-3

MyBatis SQL mapper framework for Java
http://mybatis.github.io/mybatis-3/
Apache License 2.0
19.79k stars 12.86k forks source link

java.lang.IndexOutOfBoundsException: Index: 20, Size: 20 #1567

Closed jiankunking closed 5 years ago

jiankunking commented 5 years ago
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@30c5efc1] was not registered for synchronization because synchronization is not active
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3e0207a9] was not registered for synchronization because synchronization is not active
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3e0207a9]
2019-06-14 10:16:38.821 [http-nio-8080-exec-9] WARN  com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@73e5abb0 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2019-06-14 10:16:38.830 [http-nio-8080-exec-9] WARN  com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@601b5e53 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
JDBC Connection [HikariProxyConnection@415514570 wrapping com.mysql.cj.jdbc.ConnectionImpl@1f10f2dd] will not be managed by Spring
==>  Preparing: select id, name, nickname, phone_number, phone_verified, sex, created_at, updated_at, last_login_at, password, password_strength, head_img_url, address, status, encryption_algorithm, email, email_verified, salt, position, empno from accounts where id = 10000057 
==> Parameters: 
<==    Columns: id, name, nickname, phone_number, phone_verified, sex, created_at, updated_at, last_login_at, password, password_strength, head_img_url, address, status, encryption_algorithm, email, email_verified, salt, position, empno
<==        Row: 10000057, acco7ame335+, , 19997997979, 0, 0, 2019-06-13 03:49:08, 2019-06-13 03:49:08, null, pasord@1, 90, , , 1, 9, , 0, null, , 
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@30c5efc1]
2019-06-14 10:16:38.852 [http-nio-8080-exec-9] INFO  c.jiankunking.test.account.advice.GlobalExceptionHandler - uri=/api/v1/accounts/10000057 defaultErrorHandler:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
### The error may exist in file [F:\Code\Java\jiankunking\test\account\target\classes\mapper\AccountMapper.xml]
### The error may involve com.jiankunking.test.account.dao.AccountMapper.selectByPrimaryKey
### The error occurred while handling results
### SQL: select                   id, name, nickname, phone_number, phone_verified, sex, created_at, updated_at, last_login_at, password,         password_strength, head_img_url, address, status, encryption_algorithm, email, email_verified, salt, position,         empno               from accounts         where id = 10000057
### Cause: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
    at com.sun.proxy.$Proxy94.selectOne(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:87)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58)
    at com.sun.proxy.$Proxy100.selectByPrimaryKey(Unknown Source)
    at com.jiankunking.test.account.service.impl.AccountServiceImpl.findById(AccountServiceImpl.java:61)
    at com.jiankunking.test.account.service.impl.AccountServiceImpl$$FastClassBySpringCGLIB$$23d976e7.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684)
    at com.jiankunking.test.account.service.impl.AccountServiceImpl$$EnhancerBySpringCGLIB$$faa3ea7d.findById(<generated>)
    at com.jiankunking.test.account.controller.AccountController.getAccountById(AccountController.java:62)
    at com.jiankunking.test.account.controller.AccountController$$FastClassBySpringCGLIB$$1188018d.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
    at com.jiankunking.test.account.aspect.ControllerTimeConsumingAspect.doAround(ControllerTimeConsumingAspect.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at com.jiankunking.test.account.controller.AccountController$$EnhancerBySpringCGLIB$$b2b8497c.getAccountById(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1747)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
### The error may exist in file [F:\Code\Java\jiankunking\test\account\target\classes\mapper\AccountMapper.xml]
### The error may involve com.jiankunking.test.account.dao.AccountMapper.selectByPrimaryKey
### The error occurred while handling results
### SQL: select                   id, name, nickname, phone_number, phone_verified, sex, created_at, updated_at, last_login_at, password,         password_strength, head_img_url, address, status, encryption_algorithm, email, email_verified, salt, position,         empno               from accounts         where id = 10000057
### Cause: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:76)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
    ... 83 common frames omitted
Caused by: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createUsingConstructor(DefaultResultSetHandler.java:669)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createByConstructorSignature(DefaultResultSetHandler.java:654)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:618)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:591)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:397)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:354)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:328)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:301)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:194)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:65)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
    at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
    at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
    at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
    at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:108)
    at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
    at com.sun.proxy.$Proxy148.query(Unknown Source)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
    ... 90 common frames omitted
jiankunking commented 5 years ago

实体属性必须与数据库表字段一致,不能比数据库表字段多一些属性。 Entity attributes must be consistent with database table fields and cannot have more attributes than database table fields。

harawata commented 5 years ago

Your query returns 20 columns. And I think you added @AutomapConstructor to a constructor that takes more than 20 arguments. Please let me know if I am wrong.

Assuming I am right, it may be a kind of misconfiguration, but the error message should be improved.

jiankunking commented 5 years ago

Your query returns 20 columns. And I think you added @AutomapConstructor to a constructor that takes more than 20 arguments. Please let me know if I am wrong.

Assuming I am right, it may be a kind of misconfiguration, but the error message should be improved.

I did not use @AutomapConstructor,my code is as follows: entity:

@Data
@Builder
@ApiModel(description = "账号")
public class Account implements Serializable {
    private static final long serialVersionUID = CocCoreVersion.SERIAL_VERSION_UID;
    private Long id;
    private String name;
    private String nickname;
    private String phoneNumber;
    private Boolean phoneVerified;
    private Integer sex;
    private Date createdAt;
    private Date updatedAt;
    private Date lastLoginAt;
    private String password;
    private Integer passwordStrength;
    private String headImgUrl;
    private String address;
    private Integer status;
    private List<String> customerIds;
    @ApiModelProperty(notes = "加密算法")
    @JsonIgnore
    private String encryptionAlgorithm;
    private String email;
    private Boolean emailVerified;
    private String salt;
    /**
     * 职位
     */
    private String position;
    /**
     * 员工号
     */
    private String empno;

    public AccountChangeMq toAccountChangeMq() {
        AccountChangeMq accountChangeMq = new AccountChangeMq();
        accountChangeMq.setAccountId(this.id);
        accountChangeMq.setCreatedAt(this.createdAt);
        return accountChangeMq;
    }

    public AccountDto toAccountDto() {
        AccountDto accountDto = AccountDto
                .builder()
                .address(this.address)
                .email(this.email)
                .createdAt(this.createdAt)
                .updatedAt(this.updatedAt)
                .customerIds(this.customerIds)
                .emailVerified(this.emailVerified)
                .empno(this.empno)
                .headImgUrl(this.headImgUrl)
                //.isPrimary(this.isPrimary)
                .lastLoginAt(this.lastLoginAt)
                .phoneNumber(this.phoneNumber)
                .phoneVerified(this.phoneVerified)
                .nickname(this.nickname)
                .name(this.name)
                .id(this.id)
                .build();

        return accountDto;
    }
}

mapper:

<resultMap id="AccountResultMap" type="com.jiankunking.test.account.model.po.Account">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="nickname" column="nickname"/>
        <result property="phoneNumber" column="phone_number"/>
        <result property="phoneVerified" column="phone_verified"/>
        <result property="sex" column="sex"/>
        <result property="createdAt" column="created_at"/>
        <result property="updatedAt" column="updated_at"/>
        <result property="lastLoginAt" column="last_login_at"/>
        <result property="password" column="password"/>
        <result property="passwordStrength" column="password_strength"/>
        <result property="headImgUrl" column="head_img_url"/>
        <result property="address" column="address"/>
        <result property="status" column="status"/>
        <result property="encryptionAlgorithm" column="encryption_algorithm"/>
        <result property="email" column="email"/>
        <result property="emailVerified" column="email_verified"/>
        <result property="salt" column="salt"/>
        <result property="position" column="position"/>
        <result property="empno" column="empno"/>
</resultMap>
<select id="selectByPrimaryKey" parameterType="_long" resultMap="AccountResultMap">
        select
         id, name, nickname, phone_number, phone_verified, sex, created_at, updated_at, last_login_at, password, password_strength, head_img_url, address, status, encryption_algorithm, email, email_verified, salt, position, empno
        from accounts
        where id = #{accountID,jdbcType=BIGINT}
</select>
harawata commented 5 years ago

Thanks for the code, @jiankunking .

To use the result map, MyBatis needs to instantiate the Account object first, but the only constructor has more than 20 arguments, so throwing an error is the correct behavior. Adding a no-args constructor may fix the problem (it can be private if you prefer).

p.s. Please use three backticks ``` for code blocks and check the 'Preview' before submit. Syntax highlighting is supported as well : https://guides.github.com/features/mastering-markdown#examples

jiankunking commented 5 years ago

Thank you for your advice @harawata Now that I know, it's me use of @Builder annotations that caused the problem. https://www.projectlombok.org/features/Builder

harawata commented 5 years ago

Thanks for the follow-up, @jiankunking ! Closing as it is not a bug.