gpt4 book ai didi

java - Neo4j "WHERE"中类型比较的问题

转载 作者:行者123 更新时间:2023-12-02 03:15:16 25 4
gpt4 key购买 nike

通过 java 执行 Neo4j 查询时出现以下错误:

org.neo4j.graphdb.QueryExecutionException: Don't know how to compare that. Left: "0" (String); Right: 0 (Long)
at org.neo4j.kernel.impl.query.QueryExecutionKernelException.asUserException(QueryExecutionKernelException.java:35)
at org.neo4j.cypher.internal.javacompat.ExecutionResult.converted(ExecutionResult.java:399)
at org.neo4j.cypher.internal.javacompat.ExecutionResult.hasNext(ExecutionResult.java:232)
at main.java.com.bag.server.database.Neo4jDatabaseAccess.readObject(Neo4jDatabaseAccess.java:172)
at main.java.com.bag.server.TestServer.handleNodeRead(TestServer.java:259)
at main.java.com.bag.server.TestServer.appExecuteUnordered(TestServer.java:153)
at bftsmart.tom.server.defaultservices.DefaultRecoverable.executeUnordered(DefaultRecoverable.java:417)
at bftsmart.tom.ServiceReplica.receiveReadonlyMessage(ServiceReplica.java:214)
at bftsmart.tom.core.DeliveryThread.deliverUnordered(DeliveryThread.java:289)
at bftsmart.tom.core.TOMLayer.requestReceived(TOMLayer.java:290)
at bftsmart.communication.client.netty.NettyClientServerCommunicationSystemServerSide.channelRead0(NettyClientServerCommunicationSystemServerSide.java:184)
at bftsmart.communication.client.netty.NettyClientServerCommunicationSystemServerSide.channelRead0(NettyClientServerCommunicationSystemServerSide.java:61)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:292)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:278)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:277)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:264)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:292)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:278)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:962)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:528)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:485)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:399)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:371)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
at java.lang.Thread.run(Thread.java:745)

但我确信我在数据库中有“0”作为字符串。我将其作为查询中的字符串输入:

String.format(" WHERE r.%s <= %s OR n.%s IS NULL", "snapshotId", Long.toString(0), "snapshotId")

最佳答案

您使用的是字符串修饰而不是参数,因此您丢失了类型信息(因为它只是将其全部放入字符串中)。如果您在 Cypher 中键入不带引号的字符“0”,它将被解释为 Int。因此,而不是您当前的查询,

WHERE r.snapshotId <= 0 or n.snapshotId IS NULL

你实际上想让它说

WHERE r.snapshotId <= '0' or n.snapshotId IS NULL

“0”周围有引号,因此它将其视为字符串。

不过,真正的解决方案是编写一个查询来代替字符串修改:

WHERE r.snapshotId <= {zero_string} or n.snapshotId IS NULL

然后传递一个设置为Long.toString(0)的参数zero_string。这样,驱动程序将在打包、解包和解释数据时为您处理类型。

编辑:或者如果您确实需要属性名称也是动态的,也可以将其作为参数传递:

WHERE r[{zero_param}] <= {zero_string} or n[{zero_param}] IS NULL

更新:您可以通过传入 Map 并执行一些迭代工作来修改它以适用于多个键值对。天真的做法是这样的:

WHERE ALL(k IN KEYS({map_param}) WHERE r[k] <= {map_param}[k] OR n[k] IS NULL)

但这在任何规模上都可能非常慢,因为我认为查询规划器无法对其进行优化。在应用此过滤器之前,请尝试缩小 r 与其他条件的匹配范围。

关于java - Neo4j "WHERE"中类型比较的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40404397/

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