createNativeQuery有时会抛出NullPointerException

时间:2019-09-14 12:07:45

标签: java hibernate jpa kotlin

在我的Kotlin(Ktor)应用程序中,我正在像这样运行JPA NativeQuery:

val query = em.createNativeQuery("SELECT id as chmpId, lane as position, count(*) as count, sum(games) as games FROM championstats cs WHERE cs.games >= 6 AND cs.patch = :patchid GROUP BY id, lane" +
        " UNION SELECT id as chmpId, lane as position, count(*) as count, sum(games) as games FROM championstats2 cs WHERE cs.games >= 6 AND cs.patch = :patchid GROUP BY id, lane", "SQLBuildOverview")
query.setParameter("patchid", patchid)
val results: List<SQLBuildOverview> = query.resultList as List<SQLBuildOverview>

SQLBuildOverview定义为

data class SQLBuildOverview constructor(val chmpId : Long, val position : String, val count : Int, val games : Int)

有时(这种情况很少发生),在执行此代码时,将导致类似NullPointerException的异常:(ChampionBuildDao.kt:99是我的无效代码的最后一行)

api_1          | java.lang.NullPointerException
api_1          |        at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6309)
api_1          |        at org.hibernate.loader.Loader.processResultSet(Loader.java:987)
api_1          |        at org.hibernate.loader.Loader.doQuery(Loader.java:949)
api_1          |        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
api_1          |        at org.hibernate.loader.Loader.doList(Loader.java:2692)
api_1          |        at org.hibernate.loader.Loader.doList(Loader.java:2675)
api_1          |        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2507)
api_1          |        at org.hibernate.loader.Loader.list(Loader.java:2502)
api_1          |        at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:335)
api_1          |        at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2162)
api_1          |        at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1016)
api_1          |        at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:152)
api_1          |        at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414)
api_1          |        at org.hibernate.query.Query.getResultList(Query.java:146)
api_1          |        at dao.ChampionBuildDao.findBuildStatsForPatchDB(ChampionBuildDao.kt:99)
api_1          |        at dao.ChampionBuildDao.findBuildStatsForPatch(ChampionBuildDao.kt:79)
api_1          |        at controller.ChampionController.getAllChampions(ChampionController.kt:46)
api_1          |        at controller.MainKt$main$2$3.invokeSuspend(main.kt:134)
api_1          |        at controller.MainKt$main$2$3.invoke(main.kt)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:278)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:137)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:157)
api_1          |        at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:23)
api_1          |        at io.ktor.routing.Routing.executeResult(Routing.kt:148)
api_1          |        at io.ktor.routing.Routing.interceptor(Routing.kt:29)
api_1          |        at io.ktor.routing.Routing$Feature$install$1.invokeSuspend(Routing.kt:93)
api_1          |        at io.ktor.routing.Routing$Feature$install$1.invoke(Routing.kt)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:278)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:137)
api_1          |        at io.ktor.features.CallLogging$Feature$install$2.invokeSuspend(CallLogging.kt:130)
api_1          |        at io.ktor.features.CallLogging$Feature$install$2.invoke(CallLogging.kt)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:278)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:137)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:157)
api_1          |        at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:23)
api_1          |        at io.ktor.server.engine.DefaultEnginePipelineKt$defaultEnginePipeline$2.invokeSuspend(DefaultEnginePipeline.kt:106)
api_1          |        at io.ktor.server.engine.DefaultEnginePipelineKt$defaultEnginePipeline$2.invoke(DefaultEnginePipeline.kt)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:278)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:137)
api_1          |        at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:157)
api_1          |        at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:23)
api_1          |        at io.ktor.server.netty.NettyApplicationCallHandler$handleRequest$1.invokeSuspend(NettyApplicationCallHandler.kt:31)
api_1          |        at io.ktor.server.netty.NettyApplicationCallHandler$handleRequest$1.invoke(NettyApplicationCallHandler.kt)
api_1          |        at kotlinx.coroutines.intrinsics.UndispatchedKt.startCoroutineUndispatched(Undispatched.kt:55)
api_1          |        at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:111)
api_1          |        at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:160)
api_1          |        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:54)
api_1          |        at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
api_1          |        at io.ktor.server.netty.NettyApplicationCallHandler.handleRequest(NettyApplicationCallHandler.kt:22)
api_1          |        at io.ktor.server.netty.NettyApplicationCallHandler.channelRead(NettyApplicationCallHandler.kt:16)
api_1          |        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
api_1          |        at io.netty.channel.AbstractChannelHandlerContext.access$600(AbstractChannelHandlerContext.java:38)
api_1          |        at io.netty.channel.AbstractChannelHandlerContext$7.run(AbstractChannelHandlerContext.java:353)
api_1          |        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
api_1          |        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
api_1          |        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)
api_1          |        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
api_1          |        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
api_1          |        at java.lang.Thread.run(Thread.java:745)

我的问题是什么可能导致这种错误,为什么它仅在某些时候发生?我的第一个想法是同步,因此我使运行此代码的功能同步,但收效甚微。 现在我想这可能是由于Hibernate失去数据库连接引起的吗?

我正在跑步

hibernate_version = '5.2.12.Final'
hibernate_validator_version = '6.0.7.Final'
mysql_connector_version = '5.1.46'

针对MySQL5数据库。

感谢任何帮助或想法。

0 个答案:

没有答案