2020-02-07

Spring Neo4j Auto-configuration

Neo4j is an open-source NoSQL graph database that uses a rich data model of nodes connected by
first class relationships, which is better suited for connected big data than traditional RDBMS
approaches. Spring Boot offers several conveniences for working with Neo4j, including the springboot-starter-data-neo4j “Starter”.

Connecting to a Neo4j Database

To access a Neo4j server, you can inject an auto-configured org.neo4j.ogm.session.Session. By
default, the instance tries to connect to a Neo4j server at localhost:7687 using the Bolt protocol. The
following example shows how to inject a Neo4j Session:

@Component
public class MyBean {
private final Session session;
@Autowired
public MyBean(Session session) {
this.session = session;
}
// ...
}

You can configure the uri and credentials to use by setting the spring.data.neo4j.* properties, as
shown in the following example:

spring.data.neo4j.uri=bolt://my-server:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=secret

You can take full control over the session creation by adding either an
org.neo4j.ogm.config.Configuration bean or an org.neo4j.ogm.session.SessionFactory bean.
Using the Embedded Mode
If you add org.neo4j:neo4j-ogm-embedded-driver to the dependencies of your application, Spring
Boot automatically configures an in-process embedded instance of Neo4j that does not persist any
data when your application shuts down.

As the embedded Neo4j OGM driver does not provide the Neo4j kernel itself, you
have to declare org.neo4j:neo4j as dependency yourself. Refer to the Neo4j OGM
documentation for a list of compatible versions.

The embedded driver takes precedence over the other drivers when there are multiple drivers on
the classpath. You can explicitly disable the embedded mode by setting
spring.data.neo4j.embedded.enabled=false.
Data Neo4j Tests automatically make use of an embedded Neo4j instance if the embedded driver
and Neo4j kernel are on the classpath as described above.

You can enable persistence for the embedded mode by providing a path to a
database file in your configuration, e.g.
spring.data.neo4j.uri=file://var/tmp/graph.db.

Using Native Types

Neo4j-OGM can map some types, like those in java.time.*, to String-based properties or to one of
the native types that Neo4j provides. For backwards compatibility reasons the default for Neo4j-
OGM is to use a String-based representation. To use native types, add a dependency on either
org.neo4j:neo4j-ogm-bolt-native-types or org.neo4j:neo4j-ogm-embedded-native-types, and
configure the configprop:spring.data.neo4j.use-native-types[] property as shown in the following
example:
spring.data.neo4j.use-native-types=true

Neo4jSession

By default, if you are running a web application, the session is bound to the thread for the entire
processing of the request (that is, it uses the "Open Session in View" pattern). If you do not want this
behavior, add the following line to your application.properties file:
spring.data.neo4j.open-in-view=false

Spring Data Neo4j Repositories

Spring Data includes repository support for Neo4j.
Spring Data Neo4j shares the common infrastructure with Spring Data JPA as many other Spring
Data modules do. You could take the JPA example from earlier and define City as Neo4j OGM
@NodeEntity rather than JPA @Entity and the repository abstraction works in the same way, as
shown in the following example:

package com.example.myapp.domain;
import java.util.Optional;
import org.springframework.data.neo4j.repository.*;
public interface CityRepository extends Neo4jRepository<City, Long> {
Optional<City> findOneByNameAndState(String name, String state);
}

The spring-boot-starter-data-neo4j “Starter” enables the repository support as well as transaction
management. You can customize the locations to look for repositories and entities by using

@EnableNeo4jRepositories and @EntityScan respectively on a @Configuration-bean.

No comments:

Post a Comment