(Quick Reference)

2 Getting Started - Reference Documentation

Authors: Stefan Armbruster

Version: 1.0.0

2 Getting Started

To get started with GORM for Neo4j you need to install the plugin into a Grails application:

grails install-plugin neo4j

Or configure it as a dependency in BuildConfig.groovy:

plugins {
	compile ":neo4j:latest.version"
}

By default Neo4j will used as embedded database inside the JVM, the default directory for the Neo4j datastore is data/neo4j.

2.1 Using Neo4j Standalone

If you plan to use Neo4j as your primary datastore then you need to uninstall the Hibernate plugin:

grails uninstall-plugin hibernate

Or if it has been defined in the grails-app/conf/BuildConfig.groovy file comment out the hibernate line in the plugins block

compile ":hibernate:$grailsVersion"

With this done all domain classes in grails-app/domain will be persisted via Neo4j and not Hibernate. You can create a domain class by running the regular create-domain-class command:

grails create-domain-class Person

The Person domain class will automatically be a persistent entity that can be stored in Neo4j.

2.2 Combining Neo4j And Hibernate

If you have both the Hibernate and Neo4j plugins installed then by default all classes in the grails-app/domain directory will be persisted by Hibernate and not Neo4j. If you want to persist a particular domain class with Neo4j then you must use the mapWith property in the domain class:

static mapWith = "neo4j"

Alternatively you can persist Hibernate entities to Neo4j using the special neo4j scope added to all Hibernate entities:

def hibernatePerson = Person.get(1)

hibernatePerson.neo4j.save()

def neo4jPerson = Person.neo4j.get(1)

2.3 Advanced Configuration

As mentioned the GORM for Neo4j plugin will configure all the defaults for you, but if you wish to customize those defaults you can do so in the your grails-app/conf/DataSource.groovy file:

grails {
    neo4j {
        type = "embedded"
        location = "/var/neo4j"
        params = []
    }
}

The type provides currently the following choices:

type = "embedded"

Runs the Neo4j in embedded mode, Neo4j and Tomcat use the same JVM. No seperate setup outside the Grails application is required. location specifies the directory where Neo4j stores its data.

Example:

grails {
    neo4j {
        type = "embedded"
        location = "/var/neo4j"
    }
}

If your configuration is empty, 'embedded' is used as default.

type = "rest"

Uses a org.neo4j.rest.graphdb.RestGraphDatabase instance to connect to a Neo4j database. See http://docs.neo4j.org/chunked/stable/server.html for how to setup a Neo4j server.

location specifies the URL of he Neo4j REST server. When using the Heroku neo4j addon, omit the location. In this case location will default to env.NEO4J_URL that is provided by Heroku.

Example:

grails {
    neo4j {
        type = "rest"
        location = "http://localhost:7474/db/data/"
    }
}

Additionally you must add another dependency to your application's grails-app/conf/BuildConfig.groovy:

compile("org.neo4j:neo4j-rest-graphdb:1.6")

type = "ha"

Uses a Neo4j HA setup, for details see http://docs.neo4j.org/chunked/stable/ha.html. In this case params must at least contain

For Neo4j HA either a commercial license is required, or you could use AGPL.

Example:

grails {
    neo4j {
        type = "ha"
        location = "/var/neo4j"
        params = [ // see http://docs.neo4j.org/chunked/stable/ha-configuration.html
                'ha.server_id': 1,
                'ha.coordinators': 'localhost:2181,localhost:2182,localhost:2183'
        ]
    }
}

Additionally you must add another dependency to your application's grails-app/conf/BuildConfig.groovy:

compile('org.neo4j:neo4j-enterprise:1.8.RC1')

type = "impermanent"

Uses ImpermanentGraphDatabase. This option required a dependency to artifact group: "org.neo4j", name:"neo4j-kernel", version:neo4jVersion, classifier:'tests' in BuildConfig.groovy. ImpermanentGraphDatabase is intended to be used for testing.

custom graph database

If you use a custom implementation of GraphDatabaseService, you can use
grails {
    neo4j {
        type = "my.fancy.custom.GraphDatabaseServiceImplementation"
        location = "/var/neo4j"
        params = [ :]
    }
}