Google Guice - Extensions - Persist - Neo4j

Guice Persist module to work with Neo4J OGM: https://github.com/neo4j/neo4j-ogm

License

License

Categories

Categories

Neo4J Data Databases GUI User Interface Guice Application Layer Libs Dependency Injection
GroupId

GroupId

io.innerloop
ArtifactId

ArtifactId

guice-persist-neo4j
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Google Guice - Extensions - Persist - Neo4j
Guice Persist module to work with Neo4J OGM: https://github.com/neo4j/neo4j-ogm
Project URL

Project URL

https://github.com/inner-loop/guice-persist-neo4j
Source Code Management

Source Code Management

https://github.com/inner-loop/guice-persist-neo4j

Download guice-persist-neo4j

How to add to project

<!-- https://jarcasting.com/artifacts/io.innerloop/guice-persist-neo4j/ -->
<dependency>
    <groupId>io.innerloop</groupId>
    <artifactId>guice-persist-neo4j</artifactId>
    <version>0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.innerloop/guice-persist-neo4j/
implementation 'io.innerloop:guice-persist-neo4j:0.1.0'
// https://jarcasting.com/artifacts/io.innerloop/guice-persist-neo4j/
implementation ("io.innerloop:guice-persist-neo4j:0.1.0")
'io.innerloop:guice-persist-neo4j:jar:0.1.0'
<dependency org="io.innerloop" name="guice-persist-neo4j" rev="0.1.0">
  <artifact name="guice-persist-neo4j" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.innerloop', module='guice-persist-neo4j', version='0.1.0')
)
libraryDependencies += "io.innerloop" % "guice-persist-neo4j" % "0.1.0"
[io.innerloop/guice-persist-neo4j "0.1.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.21
org.neo4j : neo4j-ogm-core jar 2.0.4
com.google.inject.extensions : guice-persist jar 4.1.0

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

guice-persist-neo4j

Guice Persist Support for Neo4j OGM.

Build Status

Quick Start

This module requires:

  • Java 8+
  • Neo4j OGM 2.0.4+
  • Google Guice Persist 4.1.0+

Install from Maven

Add the following to your <dependencies> .. </dependencies> section.

<dependency>
    <groupId>io.innerloop</groupId>
    <artifactId>guice-persist-neo4j</artifactId>
    <version>0.1.0</version>
</dependency>

Install from Gradle

Add the following to your dependencies { .. } section.

compile group: 'io.innerloop', name: 'guice-persist-neo4j', version: '0.1.0'

... or more simply:

compile: 'io.innerloop:guice-persist-neo4j:0.1.0'

Usage

Note: Please make sure you have familiarised yourself with the [Guice Persist documentation](Guice Persist).

Neo4j Object to Graph Mapping (OGM) library provides optimised mapping support between Java and installations of Neo4j utilising the Cypher query language acros a variety of protocol. It is roughly an equivalent to JPA or Hibernate. Guice Persist Neo4j extends Guice Persist to provide transactional support for your Guice applications.

##Enabling Persistence Support

To enable persistence support, simply install the Neo4j module:

Injector injector = Guice.createInjector(..., new Neo4jPersistModule("com.package.domain"));

where com.package.domain is a list of domains you want the OGM to manage.

In the OGM, you specify your configuration in an ogm.properties file at the root of the classpath. Here is an example of a simple OGM configuration for connecting with the bolt protocol:

driver=org.neo4j.ogm.drivers.bolt.driver.BoltDriver
URI=bolt://user:password@localhost

You may also configure the application directly on the module itself:

Names.bindProperties(binder(), System.getProperties());
Injector injector = Guice.createInjector(..., new Neo4jPersistModule("com.package.domain").properties(getPersistenceProperties()));

...

private static Properties getPersistenceProperties()
{
    Properties properties = new Properties();
    properties.put("neo4j.ogm.driver", System.getProperty("neo4j.ogm.driver"));
    properties.put("neo4j.ogm.url", System.getProperty("neo4j.ogm.url"));
    properties.put("neo4j.ogm.username", System.getProperty("neo4j.ogm.username"));
    properties.put("neo4j.ogm.password", System.getProperty("neo4j.ogm.password"));

    return properties;
}

Note that the way properties are configured is being reviewed and will probably be changed in an upcoming release.

Finally, you must decide when the persistence service is to be started by invoking start() on PersistService. A simple initializer class to trigger when to start is recommended:

public class MyInitializer { 
    @Inject MyInitializer(PersistService service) {
        service.start(); 

        // At this point Neo4j OGM is started and ready.
    } 
}

It makes good sense to use Guice's Service API to start all services in your application at once. In the case of web applications, this is done for you by installing the PersistFilter (see below).

##Using the OGM Session

Once you have the injector created, you can freely inject and use an EntityManager in your transactional services:

import com.google.inject.persist.Transactional;
import org.neo4j.ogm.session.Session; 

public class MyService {
    @Inject Session session; 

    @Transactional 
    public void createNewPerson() {
        session.save(new Person(...)); 
    } 
}

This is known as the session-per-transaction strategy. For more information on transactions and units of work (sessions), see this page.

Note that if you make MyService a @Singleton, then you should inject Provider<Session> instead. This is the pattern you would use when building things like Repositories.

Web Environments (session-per-http-request)

So far, we've seen the session-per-transaction strategy. In web environments this is atypical, and generally a session-per-http-request is preferred (sometimes also called open-session-in-view). To enable this strategy, you first need to add a filter to your ServletModule:

public class MyModule extends ServletModule {
  protected void configureServlets() {
    install(new Neo4jPersistModule("com.example.domain"));  // like we saw earlier.

    filter("/*").through(PersistFilter.class);
  }
}

You should typically install this filter before any others that require the Session to do their work.

Note that with this configuration you can run multiple transactions within the same Session (i.e. the same HTTP request).

Versions

Version
0.1.0