Warm tip: This article is reproduced from serverfault.com, please click

How to create queries using runtime managed labels using Neo4j-OGM?

发布于 2021-07-06 15:06:29

Simple question: I'm using Neo4-OGM (with Quarkus) to interact with my Neo4J DB (latest version).

I have an entity "Contact" and I added the @Labels to be able to manage extra labels at runtime.

@NodeEntity
public class Contact {

    @Id
    @GeneratedValue(strategy = UuidStrategy.class)
    private String identifier;

    // some properties and relations...

    @Labels
    private List<String> labels;

}

This will work fine.

But now, I would like to querying my DB using the methods loadAll with Filters instead of writing by myself a cypher query.

Unfortunately, I cannot see how I could get any equivalent of the following cypher query:

MATCH (n:`Contact`:`Label_added_in_labels`) RETURN n

Is it supported? Or I will have to write the cypher by myself? (That's fine but I don't want to write them if it's not needed).

Questioner
koD
Viewed
0
meistermeier 2021-07-29 14:08:17

The Filter in Neo4j-OGM are property based and sadly cannot help you with this. But you could use the Neo4j CypherDSL if you do not want to write your own statements.

For this you can add the following dependency to your project

<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-cypher-dsl</artifactId>
    <version>2021.3.0</version> // <- currently the latest version
</dependency>

and use it for example like this in combination with a Neo4j-OGM Session:

Node node = Cypher.node("Contact", "Label_added_in_labels");
Statement statement = Cypher.match(node.named("n")).returning(node).build();
Iterable<User> contacts = session.query(Contact.class, statement.getCypher(), Collections.emptyMap());