通过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")
答案 0 :(得分:1)
您正在使用字符串修改而不是参数,因此您丢失了类型信息(因为它只是将它全部放在一个字符串中)。如果您输入字符&#39; 0&#39; 0在Cypher中没有引号,它将被解释为Int
。因此,而不是您当前的查询,
WHERE r.snapshotId <= 0 or n.snapshotId IS NULL
你实际上想要它说
WHERE r.snapshotId <= '0' or n.snapshotId IS NULL
使用&#39; 0&#39;周围的引号,因此它将其视为字符串。
然而,真正的解决方案是代替字符串修改,写一个查询来说:
WHERE r.snapshotId <= {zero_string} or n.snapshotId IS NULL
然后传递设置为zero_string
的参数Long.toString(0)
。这样,驱动程序将在打包,解包和解释数据时为您处理类型。
编辑:或者如果您确实需要属性名称也是动态的,也可以将其作为参数传递:
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
匹配其他一些标准。
答案 1 :(得分:1)
format
语句生成的字符串为:
" WHERE r.snapshotId <= 0 OR n.snapshotId IS NULL"
就Cypher而言,Long.toString(0)
值仍会导致输出数字0
,因为字面值未被引号括起。
要解决此问题,您必须确保所比较的值具有兼容的类型。
一种方法是确保数值表示为字符串(通过引用它):
String.format(" WHERE r.%s <= '%s' OR n.%s IS NULL", "snapshotId", 0, "snapshotId")
但是,如果您希望比较结果在算术上有意义,那么使用字符串比较通常没有意义。例如,(".1" <= "0")
为true
,而(.1 <= 0)
为false
。因此,对于在算术上有意义的比较,您应该将r.snapshotId
转换为浮点数或整数。
例如:
String.format(" WHERE TOFLOAT(r.%s) <= %s OR n.%s IS NULL", "snapshotId", 0, "snapshotId")
最后,正如@ToreEschliman建议的那样,你应该传递参数而不是使用字符串修改。