gpt4 book ai didi

java - EntityManager..getResultList() 中的 ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-11-29 03:43:03 26 4
gpt4 key购买 nike

我在 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); 

最佳答案

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

关于java - EntityManager..getResultList() 中的 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10755191/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com