简体   繁体   English

Spring Data Neo4j ogm会话问题与id和深度的loadAll

[英]Spring Data Neo4j ogm Session issue with loadAll by id and depth

I have a graph similar to this: 我有一个与此类似的图:

(t:Teacher)-[:TEACHES]-(s:Student)-[:ATTENDS]-(s:Subject)-[:REQUIRES]-(b:Book)

expressed in @NodeEntity classes. @NodeEntity类表示。

I need to load all @NodeEntities for teachers with Ids withing a given range, and fetch all students taught by them, with their Subjects and the Books required by each Subject, all in one query. 我需要在给定范围内为具有ID的教师加载教师的所有@NodeEntities ,并在一个查询中获取所有由他们教过的学生以及他们的主题和每个主题所需的书籍。

I tried 我试过了

int depth=3;
List<Long> ids = newArrayList(3,4,5);
session.loadAll(Teacher.class, ids, depth);

I actually started with the corresponding GraphRepository findAll it is the same thing. 其实,我开始与相应GraphRepository findAll这是同样的事情。

Unfortunately I get a list of all available Teachers in the database, because each student happens to be taught by all teachers. 不幸的是,我得到了数据库中所有可用教师的列表,因为每个学生碰巧都由所有教师教。 I also tried running a custom @Query though it appears that you cannot specify custom depth How to control depth on custom Spring Data Neo4j repository methods? 我也尝试运行自定义@Query,尽管它似乎无法指定自定义深度。 如何控制自定义Spring Data Neo4j存储库方法的深度? nor return more than one node from custom cypher query. 也不要从自定义密码查询中返回多个节点。

Do you have an idea why my approach is wrong? 您是否知道为什么我的方法是错误的? This seams to be a simple use of neo4j and I'm stuck with fetching my whole graph. 这似乎是neo4j的一种简单用法,我被困于获取整个图。

Yes, when you call session.loadAll with your model to depth 3 you are basically returning all teachers as there is no pattern match applied to the load query to restrict it to the nodes you are talking about. 是的,当您在模型达到深度3的情况下调用session.loadAll ,您基本上将返回所有教师,因为没有将模式匹配应用于负载查询以将其限制为您正在讨论的节点。 So thinking about this from the perspective of depth isn't really going to help what you are trying to do. 因此,从深度的角度考虑这个问题并不会真正帮助您尝试做的事情。

There are two (albeit similar) ways to get this working. 有两种(尽管相似)方法可以使此工作正常进行。

a) You correctly mention using a custom @Query . a)您使用自定义@Query正确提及。 This is the easiest way and you don't need to specify depth. 这是最简单的方法,您无需指定深度。 In order for the query to work you must also return your relationships. 为了使查询正常工作,您还必须返回您的关系。 Your signature would be something like this: 您的签名将如下所示:

    @Query("MATCH (t:Teacher)-[teacherRels:TEACHES]-(st:Student)-[studentRels:ATTENDS]-(su:Subject)-[subjectRels:REQUIRES]-(b:Book) WHERE ID(t) in {ids} RETURN t, teacherRels, st, studentRels, su, subjectRels, b")
    public Iterable<Teacher> findTeachersByIds(@Param("ids") List<Long> ids);

b) You can do the same thing as above with session.query instead (which is what the above method will basically call). b)您可以改为使用session.query与上述相同的操作(上述方法基本上会调用该方法)。

If you do want to upgrade you should use 4.2.0.RC1 which should be out on 28/11/2016 (You can use 4.2.0.BUILD-SNAPSHOT in the mean time if you want. It's more stable than 4.2.0.M1 ). 如果确实要升级,则应使用4.2.0.RC1 ,该版本应于4.2.0.RC1年11月28日发布(如果需要,您可以同时使用4.2.0.BUILD-SNAPSHOT 。它比4.2.0.M1稳定。 4.2.0.M1 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM