DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Data Engineering
  3. Databases
  4. Neo4j Object Mapping With GORM, Groovy, and Spring Boot

Neo4j Object Mapping With GORM, Groovy, and Spring Boot

Using Groovy with Neo4j is now easier than ever with GORM for Neo4j 6.1. Read on for details and to show you how you can leverage this library in your next app.

Graeme Rocher user avatar by
Graeme Rocher
·
Apr. 20, 17 · Tutorial
Like (2)
Save
Tweet
Share
5.19K Views

Join the DZone community and get the full member experience.

Join For Free
Learn how to use GORM, Groovy and Spring Boot for object mapping in Neo4j

Mapping object models to the Neo4j 

graph in languages such as Java and Groovy may be immensely convenient when developing your application, but has the danger of obfuscating the power of Neo4j.

With the recently released GORM for Neo4j 6.1 at OCI, the home of the core Grails team, we built a tool that takes advantage of the native features exposed by the Neo4j Bolt Java Driver.

Get Started

If you want to get started immediately, we’ve provided 

a detailed tutorial 

that walks through all the features that GORM provides for Neo4j.

You’ll use GORM and Grails to develop a simple web application for the built-in movie database. The guide covers the backend with domain model and services, which provide REST endpoints to a JavaScript UI.

A repository with the resulting Grails application 

is available here

.

If you’d rather use Spring Boot with Web-MVC, GORM provides you the same benefits. Here is a repository for an example Spring Boot application that achieves the same thing.

But now, let’s look at GORM for Neo4j in some more detail.

Model Nodes, Relationships, and Paths

GORM for Neo4j allows you to model 

not just nodes

, but also to 

map Groovy classes to relationships

.

Combine all of this with the supporting features that you would expect from an object mapping tool, such as:

  • Dirty checking and optimized updates.
  • Batch writes with UNWIND
  • Transaction management.
  • Customizable ID generation.
  • Optimistic locking.
  • Lazy loading.
For example, to map a Groovy class to a graph node simply define a class and annotated it with @Entity:

import grails.gorm.annotation.*
@Entity
class Movie {
    String title
    String tagline
    int released
}

Relationships can be modeled by using the

grails.neo4j.Relationship

trait:

import grails.neo4j.Relationship

@Entity
class CastMember implements Relationship

      { 
    List

       roles = [] } 

These can then be queried either natively or using GORM’s 

existing rich query API

.

Later you can use more advanced GORM features like constraints or has-many mappings.

class Movie {
    String title
    String tagline
    int released

    static hasMany = [cast: CastMember]

    static constraints = {
        released min:1900
        title blank:false
    }
}

Groovy and Neo4j: A Natural Fit

With Neo4j being a schema-less database, Groovy’s ability to simplify dynamic code (whilst still providing support for static compilation) really shines.

You can of course statically declare the properties of your domain class, but if you need to assign dynamic properties to individual objects, that is not a problem with Groovy.

For example, you can read and write dynamic properties using Groovy’s subscript operator, thus not limiting yourself to a statically defined schema:

Movie movie = Movie.findByTitle("The Matrix")
movie["rating"] = 5

println "The movie ${movie} is rated ${movie['rating']}"

Harness the Power of Cypher

GORM for Neo4j provides many 

awesome ways to build queries

, though none of these impacts your ability to use the native power of 

Cypher

.

You can use Cypher for all your queries, or just to solve specific problems, such as finding the shortest path.

You can even get GORM to write your Cypher query logic for you, using 

GORM Data Services

!

For example the following interface uses the @Cypher annotation to automatically implement a method called updatePerson for you.

import grails.gorm.services.Service
import grails.neo4j.services.Cypher

@Service(Person)
interface PersonService {

    @Cypher("""MATCH ${Person p} 
               WHERE ${p.name} = ${name}  
               SET ${p.age} = ${age}""")
    void updatePerson(String name, int age)
}


Note: 

Compile time checking is applied against the classes and property references including within the Cypher query!

Built for the Real World

Our preference at 

OCI 

is to always build solutions based on real-world experiences, and with GORM for Neo4j that hasn’t changed.

We worked together with Brinqa – a leading provider of unified risk management – to build an object mapping solution for Neo4j that addresses their real-world challenges.

Curious to know more? We have prepared 

a detailed tutorial 

based on the original Neo4j movies data model that demonstrates building an application with Grails, GORM 6.1 and Neo4j. Please check it out and 

let us know if you have any suggestions for improvement

.

[As community content, this post reflects the views and opinions of the particular author and does not necessarily reflect the official stance of Neo4j.]

Spring Framework Neo4j Gorm (computing) Spring Boot Groovy (programming language) Object (computer science) Database application Grail (web browser)

Published at DZone with permission of Graeme Rocher. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • JWT Authentication and Authorization: A Detailed Introduction
  • Building the Next-Generation Data Lakehouse: 10X Performance
  • A Beginner's Guide to Infrastructure as Code
  • 5 Software Developer Competencies: How To Recognize a Good Programmer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: