This short blog post explains how to use Cypher Querydsl with eXtended Objects and Neo4j Datastore. Read the “Getting started with Composite Data Objects and Neo4j” if you are not familiar with the eXtended Objects Framework. The project has been renamed to “eXtended Objects” since this “Getting Started” blog post has been published – just in case you wonder about the different name.

Simple Example

A simple example might be an entity Person that looks as follows:

@Label("Person")
public interface Person {
    @Indexed
    String getName();
    void setName(String name);
}

Adding the required dependencies

Add the following dependencies to the dependencies section of your Maven project:

<properties>
    <querydsl.version>3.3.4</querydsl.version>
</properties>

<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <version>${querydsl.version}</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.6.1</version>
</dependency>
<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-cypher-dsl</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
    <groupId>com.mysema.querydsl</groupId> 
    <artifactId>querydsl-lucene3</artifactId>
    <version>${querydsl.version}</version>
    <optional>true</optional>
    <exclusions>
        <exclusion>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
        </exclusion>
    </exclusions>
</depdendency>
<dependency>
    <groupId>com.smb-tec.xo</groupId>
    <artifactId>querydsl-xo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

Configuring the APT Maven Plugin

We now have to configure the APT Maven Plugin which looks as follows:

<plugin>
    <groupId>com.mysema.maven</groupId>
    <artifactId>maven-apt-plugin</artifactId>
    <version>1.0.4</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <!-- the directory in which the query types get generated -->
                <outputDirectory>target/generated-sources/annotations</outputDirectory>
                <!-- the annotation processor to be used -->
                <processor>com.smbtec.xo.querydsl.apt.XONeo4jAnnotationProcessor</processor>
            </configuration>
        </execution>
    </executions>
</plugin>

Now start the source generation by running

mvn generate-sources

After the generate-sources goal ran successfully you can find the newly created query types in the configured outputDirectory. In our example QPerson.java is generated which can be used by our sample application as follows:

// initialize XOManager object
final XOManager xoManager = getXoManager();
xoManager.currentTransaction().begin();

// create a Person object with name "Morpheus"
Person person = xoManager.create(Person.class);
person.setName("Morpheus");

// create a query object by using the fluent Cypher DSL
QPerson p = QPerson.person;
Execute execute = start(allNodes(identifier(p)))
    .where(toBooleanExpression(p.name.eq("Morpheus")))
    .returns(identifier(p));

// execute the built Cypher query by using XO API
Person result = xoManager.createQuery(execute.toString(), Person.class)
    .execute().getSingleResult();

// validate the result
assertThat(person, equalTo(result));
xoManager.currentTransaction().commit();

Summary

The XO Annotation Processor lets you generate Querydsl query types which can easily be used to build Cypher queries by using the Cypher-DSL developed by the Neo4j team.