Starting with Neo4J-Maven-Spring

Last change 12/04/2014

Using neo4j is pretty easy - you got nodes and properties and those nodes have relations. Doing a Mapping to pojos can be done quickly too - using spring-data-neo4j. And starting a new project is as easy as it goes with maven.

Maven Setup

Maven was pretty straight foreward. Just add the neo4j releases repo and add the dependencies in your pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

...
    <dependencies>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-cypher</artifactId>
            <version>1.6</version>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-kernel</artifactId>
            <version>1.6</version>
            <classifier>tests</classifier>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-rest-graphdb</artifactId>
            <version>1.6</version>
        </dependency>
        <!-- neo4j + spring -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j</artifactId>
            <version>2.0.0.RELEASE</version>
            <exclusions>
                <exclusion>
                    <artifactId>neo4j</artifactId>
                    <groupId>org.neo4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j-rest</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>neo4j-releases</id>
            <url>http://m2.neo4j.org/content/repositories/releases</url>
        </repository>
    </repositories>
</project>

Spring Setup

Getting neo4j with spring was extremely simple. I just had to add it to the applicationContext.xml<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
             http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
    default-lazy-init="true">

    <import resource="classpath:daoContext.xml" />
    <import resource="classpath:serviceContext.xml" />

    <neo4j:config storeDirectory="data/graph.db" />
</beans>

My first Neo4J app (Standalone)

Same thing - but with a servlet on tomcat

Testing neo4j - junit

to get a in-memory neo4j database I had to create a applicationContext.xml for testing:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
             http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
    default-lazy-init="true">

    <import resource="classpath:daoContext.xml" />
    <import resource="classpath:serviceContext.xml" />

    <neo4j:config graphDatabaseService="graphDatabaseService"/>
    <!--  use in memory graph database -->
    <bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase"/>

</beans>

Site created with Corinis CCM