EntityManager..getResultList()中的ArrayIndexOutOfBoundsException

时间:2012-05-25 13:21:57

标签: java mysql hibernate jpa persistence

我遇到了java中的持久性问题。

这是抛出异常的方法

public List<EscaleCatalogueKaravel> obtenirListeEscalesCroisiereAvecIdCroisiereKaravel(Integer idCroisiereKaravel, Integer refLangue) {
         List<EscaleCatalogueKaravel> listeEscales = entityManager.createQuery("select p from EscaleCatalogueKaravel p " +
                    "where p.idCroisiereComplete=:idCroisiereComplete and p.refLangue=:refLangue")
                    .setParameter("idCroisiereComplete", idCroisiereKaravel)
                    .setParameter("refLangue", refLangue)
                    .getResultList();

         if (listeEscales == null || listeEscales.size() == 0) {
            return null;
         }

         return listeEscales;
    }

问题在于它确实有效,但有时会在 getResultList()中崩溃:

这是由于该方法生成的List类型?无论如何,工作查询和崩溃的查询之间没有太大的区别。我的意思是他们都有或多或少相同数量的结果。

有什么建议吗?

java.lang.ArrayIndexOutOfBoundsException: 1
    at com.mysql.jdbc.StringUtils.getInt(StringUtils.java:702)
    at com.mysql.jdbc.StringUtils.getInt(StringUtils.java:771)
    at com.mysql.jdbc.ByteArrayRow.getInt(ByteArrayRow.java:109)
    at com.mysql.jdbc.ResultSetImpl.getIntWithOverflowCheck(ResultSetImpl.java:7209)
    at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2699)
    at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2814)
    at org.hibernate.type.IntegerType.get(IntegerType.java:28)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
    at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2101)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1380)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)
    at org.hibernate.loader.Loader.getRow(Loader.java:1206)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
    at org.hibernate.loader.Loader.doQuery(Loader.java:701)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    at org.hibernate.loader.Loader.doList(Loader.java:2213)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
    at org.hibernate.loader.Loader.list(Loader.java:2099)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:66)
    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.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:310)
    at $Proxy33.getResultList(Unknown Source)
    at com.karavel.connectivity.gateway.croisiere.common.repository.impl.CatalogueKaravelRepositoryImpl.obtenirIdCroisiereCompleteAvecInfoCroisiere(CatalogueKaravelRepositoryImpl.java:75)
    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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
    at $Proxy23.obtenirIdCroisiereCompleteAvecInfoCroisiere(Unknown Source)
    at com.karavel.connectivity.gateway.croisiere.controller.service.RechercherCroisiereController.getItineraireCroisiere(RechercherCroisiereController.java:378)
    at com.karavel.connectivity.gateway.croisiere.controller.service.RechercherCroisiereController.obtenirListeCroisiere(RechercherCroisiereController.java:283)

编辑以增加更多精度:

如果我更深地调试一个垃圾,我会看到抛出异常的方法

类org.springframework.orm.jpa.SharedEntityManagerCreator.java

private static class DeferredQueryInvocationHandler implements InvocationHandler {

        private final Query target;

        private final EntityManager em;

        public DeferredQueryInvocationHandler(Query target, EntityManager em) {
            this.target = target;
            this.em = em;
        }

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            // Invocation on Query interface coming in...

            if (method.getName().equals("equals")) {
                // Only consider equal when proxies are identical.
                return (proxy == args[0]);
            }
            else if (method.getName().equals("hashCode")) {
                // Use hashCode of EntityManager proxy.
                return hashCode();
            }
            else if (method.getName().equals("unwrap")) {
                // Handle JPA 2.0 unwrap method - could be a proxy match.
                Class targetClass = (Class) args[0];
                if (targetClass == null || targetClass.isInstance(proxy)) {
                    return proxy;
                }
            }

            // Invoke method on actual Query object.
            try {
                Object retVal = method.invoke(this.target, args);
                return (retVal == this.target ? proxy : retVal);
            }
            catch (InvocationTargetException ex) {
                throw ex.getTargetException();
            }
            finally {
                if (method.getName().equals("getResultList") || method.getName().equals("getSingleResult") ||
                        method.getName().equals("executeUpdate")) {
                    EntityManagerFactoryUtils.closeEntityManager(this.em);
                }
            }
        }
    }

确切地说:

Object retVal = method.invoke(this.target, args); 

1 个答案:

答案 0 :(得分:0)

我唯一能想到的是你的列表不是null并且包含一个元素。在这种情况下,array.get(1)将导致错误,因为array.get(0)是第一个元素。也许您可以记录数组大小,看看大小是否符合您的期望。