Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Minimal Kotlin/Gradle Example for Neo4j OGM
package so
import org.neo4j.ogm.annotation.GeneratedValue
import org.neo4j.ogm.annotation.Id
import org.neo4j.ogm.annotation.NodeEntity
import org.neo4j.ogm.annotation.Relationship
import org.neo4j.ogm.config.Configuration
import org.neo4j.ogm.session.SessionFactory
@NodeEntity
class Actor(var name: String) {
@Id
@GeneratedValue
var id: Long? = null
@Relationship(type = "ACTS_IN", direction = "OUTGOING")
var movies = mutableSetOf<Movie>()
fun actsIn(movie: Movie) {
movies.add(movie)
movie.actors.add(this)
}
}
@NodeEntity
class Movie(var title: String, var released: Int) {
@Id
@GeneratedValue
var id: Long? = null
@Relationship(type = "ACTS_IN", direction = "INCOMING")
var actors = mutableSetOf<Actor>()
}
object Neo4j {
val configuration = Configuration.Builder()
.uri("bolt://localhost")
.credentials("neo4j", "test")
.build()
val sessionFactory = SessionFactory(configuration, "so")
fun save() {
val session = sessionFactory.openSession()
val develsAdvocate = Movie("The Devil’s Advocate", 1997)
var theMatrix = Movie("The Matrix", 1999)
Actor("Carrie-Anne Moss").actsIn(theMatrix)
val keanu = Actor("Keanu Reeves")
keanu.actsIn(theMatrix)
keanu.actsIn(develsAdvocate)
session.save(develsAdvocate)
session.save(theMatrix)
session.clear()
val theMatrixReloaded = session.load(Movie::class.java, theMatrix.id, 2)
println("Found ${theMatrixReloaded.title}")
for (actor in theMatrixReloaded.actors) {
println("Actor ${actor.name} acted in ${actor.movies.joinToString { it.title }}")
}
sessionFactory.driver.close()
}
}
fun main(args: Array<String>) {
val neoInstance = Neo4j
neoInstance.save()
}
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-noarg:1.2.51"
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.2.51'
}
apply plugin: 'kotlin'
apply plugin: "kotlin-noarg"
group 'soquestions'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile 'org.neo4j:neo4j-ogm-core:3.1.2'
compile 'org.neo4j:neo4j-ogm-bolt-driver:3.1.2'
compile 'org.slf4j:slf4j-simple:1.7.25'
}
noArg {
annotation("org.neo4j.ogm.annotation.NodeEntity")
annotation("org.neo4j.ogm.annotation.RelationshipEntity")
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment