使用过程中遇到的问题记录
1、使用 $param 参数问题,如查询某些标签时不需要自动加双引号,如 match(n:label),而lable作为参数时,会自动加双引号变为 match(n:"label"), 这就会导致脚本出错,如代码中的参数 knowledgePointLabel 被加了双引号导致查询错误
java
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
/**
* 知识词条 持久层
*
* @author kou
*/
@Repository
public interface KnowledgeEntryRepository extends Neo4jRepository<KnowledgeEntry, Long> {
/**
* 统计文献关联的知识词条下的知识点数量
* (文献)-[r:`知识词条`]->(知识点)
*
* @param literatureId 文献id
* @param knowledgePointLabel 知识点label
* @return 结果
*/
@Query("MATCH(n:`文献`) " +
"where id(n) = $literatureId " +
"with n " +
"match (n)-[r:`知识词条`]-(k:`$knowledgePointLabel`) " +
"return count(k) ")
int countLiteratureKnowledgePoints(@Param("literatureId") Long literatureId, @Param("knowledgePointLabel") String knowledgePointLabel);
}
解决方法:
1、使用cypher 脚本执行,进行拼接
2、更新cypher 脚本
java
@Query("MATCH(n:`文献`),(k) " +
"where id(n) = $literatureId " +
"and [x in labels(k) where x = $knowledgePointLabel] " +
"with n,k " +
"match (n)-[r:`知识词条`]-(k) " +
"return count(k) ")