Spring Data Neo4j Parent Child过滤

时间:2016-03-25 05:38:44

标签: java spring neo4j spring-data-neo4j spring-data-neo4j-4

enter image description here

我有如上图所示的节点关系

我的课程

@NodeEntity
public class User 
{


    @GraphId
    private Long id;

    @Property(name="name")
    private String name;


    @Relationship(type = "CAN_ACCESS", direction = Relationship.OUTGOING)
    private List<Library> libraries;


    // is this necessary ?????
    @Relationship(type = "CAN_ACCESS", direction = Relationship.OUTGOING)
    private List<Book> books;


    public User() 
    {

    }

    // getters
    // setters  
}


@NodeEntity
public class Library 
{
    @GraphId
    private Long id;

    @Property(name="name")
    private String name;


    @Relationship(type = "CONTAINS", direction = Relationship.OUTGOING)
    private List<Book> books;

    // is this necessary ?????
    @Relationship(type = "CAN_ACCESS", direction = Relationship.INCOMING)
    private List<User> users;


    public Library() 
    {

    }

    // getters
    // setters  
}

@NodeEntity
public class Book 
{
    @GraphId
    private Long id;

    @Property(name="name")
    private String name;

    // is this necessary ?????
    @Relationship(type = "CONTAINS", direction = Relationship.INCOMING)
    private Library library;

    // is this necessary ?????
    @Relationship(type = "CAN_ACCESS", direction = Relationship.INCOMING)
    private List<User> users;


    public Book() 
    {

    }

    // getters
    // setters  
}

我有用户节点Id = 21和库节点Id = 32.我想查询属于图书馆32但只有用户21可以访问的图书。

注意 - 尽管用户21&#34; CAN_ACCESS&#34;图书馆32,这并不意味着他&#34; CAN_ACCESS&#34;所有书籍&#34; CONTAINS&#34;在图书馆32

我在服务类中的当前方法是

@Autowired
private LibraryRepository libraryRepository;

@Autowired
private UserRepository userRepository;

@Autowired
private BookRepository bookRepository;

@Autowired
private Session session;


public void testGraph()
{
    Long userId = 21;
    Long libId = 32;
    int depth = 1;

    Library library = libraryRepository.findOne(32,depth);
    List<Book> books = library.getBooks();
    List<Book> userAccessibleBooks = getUserAccessibleBooks(books,userId);
}

public List<Book> getUserAccessibleBooks(List<Book> books,Long userId)
{
    // Loop over book list and get List<User> users;
    // check this user "CAN_ACCESS" the book
    // return accessible book list
}

我不认为这是最好的方法。假设你有数百万本图书馆藏书

任何解决方案?或者我在Spring数据neo4j(过滤器)中遗漏了什么?

摘要

我希望将带有过滤的图书清单的图书馆32作为儿童用户21和#34; CAN_ACCESS&#34;

1 个答案:

答案 0 :(得分:1)

您希望将存储库查找器用于此类查询。您想要返回List<Book>,这将进入BookRepository。

最简单的方法是定义一个密码查询:

MATCH (u:User), (l:Library)
WHERE 
ID(u) = {userId} AND ID(u) = {libraryId}
MATCH 
(u)-[:CAN_ACCESS]->(b:Book),
(l)-[:CONTAINS]-(b)
RETURN b

实际上并不推荐使用原生图形ID,因此如果您引入,例如uuid查询可能如下所示:

MATCH 
(u:User {uuid:{userUuid}}),
(l:Library {uuid:{libraryUuid}}),
(u)-[:CAN_ACCESS]->(b:Book),
(l)-[:CONTAINS]-(b)
RETURN b

然后将此查询的finder方法添加到BookRepository接口:

@Query("MATCH 
(u:User {uuid:{userUuid}}),
(l:Library {uuid:{libraryUuid}}),
(u)-[:CAN_ACCESS]->(b:Book),
(l)-[:CONTAINS]-(b)
RETURN b")
List<Book> findBooksByUserAndLibrary(@Param("userUuid") String userUuid, @Param("libraryUuid) String libraryUuid);