Spring Repository方法抱怨找不到字段

时间:2015-06-18 12:13:40

标签: spring spring-data spring-data-jpa spring-data-neo4j

我在我的项目中使用Spring Data Neo4j,而且我遇到了存储库命名约定的问题。

这是一个只包含一个字段和getters / setters

的简单类
@RelationshipEntity
public class ScoredRelationship
{
  protected Float score;
}

并且下面的类用其他类型的字段扩展它

@RelationshipEntity(
    type = RecommenderRelTypes.GOV_CONSUMER_TO_GOV_CONSUMER_SIMILARITY)
public class GovConsumerToGovConsumerSimilarity extends ScoredRelationship
{ // Other fields}

要访问该关系,我正在使用通常的存储库类

public interface GovConsumerToGovConsumerSimilarityRepository extends
    GraphRepository<GovConsumerToGovConsumerSimilarity>
{

  public Set<GovConsumerToGovConsumerSimilarity> findByScoreGreaterThan(Float value);

  public Set<GovConsumerToGovConsumerSimilarity>
     findByScoreGreaterThanOrderByScoreDesc(Float value);

  public Set<GovConsumerToGovConsumerSimilarity>
     findTopXByScoreGreaterThanOrderByScoreDesc(int limit, Float score);
}

此代码编译良好。但是,每当我尝试使用其中一种方法时,Spring都会返回一系列异常或不按预期行事。

F.e。 #findByScoreGreaterThan(0.3f)始终返回空集。但是,调用findAll()并打印所有分数时,它实际上有很多分数大于0.3的对象。

在第二和第三种情况下,它总是抛出一个例外

Caused by: Unknown identifier `score`.
    at org.neo4j.cypher.internal.symbols.SymbolTable.evaluateType(SymbolTable.scala:60)
    at org.neo4j.cypher.internal.commands.expressions.Identifier.evaluateType(Identifier.scala:51)
    at org.neo4j.cypher.internal.commands.expressions.Expression.assertTypes(Expression.scala:53)
    at org.neo4j.cypher.internal.pipes.SortPipe$$anonfun$assertTypes$1.apply(SortPipe.scala:34)
    at org.neo4j.cypher.internal.pipes.SortPipe$$anonfun$assertTypes$1.apply(SortPipe.scala:33)
    at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
    at scala.collection.immutable.List.foreach(List.scala:45)
    at org.neo4j.cypher.internal.pipes.SortPipe.assertTypes(SortPipe.scala:33)
    at org.neo4j.cypher.internal.pipes.PipeWithSource.<init>(PipeWithSource.scala:27)
    at org.neo4j.cypher.internal.pipes.SortPipe.<init>(SortPipe.scala:29)
    at org.neo4j.cypher.internal.executionplan.builders.SortBuilder.apply(SortBuilder.scala:33)
    at org.neo4j.cypher.internal.executionplan.ExecutionPlanImpl.prepareExecutionPlan(ExecutionPlanImpl.scala:49)
    at org.neo4j.cypher.internal.executionplan.ExecutionPlanImpl.<init>(ExecutionPlanImpl.scala:33)
    at org.neo4j.cypher.ExecutionEngine$$anonfun$prepare$1.apply(ExecutionEngine.scala:67)
    at org.neo4j.cypher.ExecutionEngine$$anonfun$prepare$1.apply(ExecutionEngine.scala:67)
    at org.neo4j.cypher.internal.LRUCache.getOrElseUpdate(LRUCache.scala:37)
    at org.neo4j.cypher.ExecutionEngine.prepare(ExecutionEngine.scala:67)
    at org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:59)
    at org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:63)
    at org.neo4j.cypher.javacompat.ExecutionEngine.execute(ExecutionEngine.java:79)
    at org.springframework.data.neo4j.support.query.CypherQueryEngine.parseAndExecuteQuery(CypherQueryEngine.java:61)

怎么可能?我的意思是,班级显然有score字段。此外,执行简单#findByScoreGreaterThan(float value)不会抛出任何异常,但同样后一种方法总是返回一个空集。

编辑: 这些是Spring使用的查询。实际上,他们似乎是对的

Executing cypher query: START `govConsumerToGovConsumerSimilarity`=node:__types__(className="it.cerict.recommender.persistence.neo4j.GovConsumerToGovConsumerSimilarity") WHERE `govConsumerToGovConsumerSimilarity`.`score`! > {0} RETURN `govConsumerToGovConsumerSimilarity` params {0=0.3}

Executing cypher query: START `govConsumerToGovConsumerSimilarity`=node:__types__(className="it.cerict.recommender.persistence.neo4j.GovConsumerToGovConsumerSimilarity") WHERE `govConsumerToGovConsumerSimilarity`.`score`! > {0} RETURN `govConsumerToGovConsumerSimilarity` ORDER BY score DESC params {0=0.3}

EDIT2:我还尝试将score类型从Float更改为float,但没有进一步改进。

1 个答案:

答案 0 :(得分:1)

这似乎是与Spring Data Neo4j相关的错误。查看执行的查询,很明显它会搜索节点,而它应该搜索关系。

我使用#findByScoreGreaterThanOrderByScoreDesc()注释更改了@Query方法,该注释指定了以下Cypher查询

START `govConsumerToGovConsumerSimilarity`=rel:__rel_types__(className="it.cerict.recommender.persistence.neo4j.GovConsumerToGovConsumerSimilarity") WHERE `govConsumerToGovConsumerSimilarity`.`score`! > 0.3 RETURN `govConsumerToGovConsumerSimilarity` ORDER BY `govConsumerToGovConsumerSimilarity`.`score` DESC;