作为上面的磁贴,我一直在尝试使用ne4j-ogm和kotlin,但没有成功。如果我试图持久化我的数据,Neo4j抛出一个异常,“类xxxx不是一个有效的实体”。
package com.asofttz.micros.administrator.users.testmodels
import org.neo4j.ogm.annotation.GeneratedValue
import org.neo4j.ogm.annotation.Id
import org.neo4j.ogm.annotation.NodeEntity
import org.neo4j.ogm.annotation.Relationship
@NodeEntity
class Actor(var name: String = "") {
@Id
@GeneratedValue
open var id: Long? = null
@Relationship(type = "ACTS_IN", direction = "OUTGOING")
open val movies = hashSetOf<Movie>()
fun actsIn(movie: Movie) {
movies.add(movie)
movie.actors.plus(this)
}
}
@NodeEntity
class Movie(var title: String = "", var released: Int = 2000) {
@Id
@GeneratedValue
open var id: Long? = null
@Relationship(type = "ACTS_IN", direction = "INCOMING")
open var actors = setOf<Actor>()
}
有没有附近的路?有没有使用kotlin将数据持久化到Neo4j数据库的替代方案?
N:B。我使用的是kotlin版本1.2.60和Neo4j-OGM v3.2.1
更新
下面是我剩余的代码
import com.asofttz.micros.administrator.users.testmodels.Actor
import com.asofttz.micros.administrator.users.testmodels.Movie
import org.neo4j.ogm.config.Configuration
import org.neo4j.ogm.session.SessionFactory
import java.util.*
object Neo4j {
val configuration = Configuration.Builder()
.uri("bolt://localhost")
.credentials("neo4j", "password")
.build()
val sessionFactory = SessionFactory(configuration, "test.movies.domain")
fun save() {
val session = sessionFactory.openSession()
val movie = Movie("The Matrix", 1999)
session.save(movie)
val matrix = session.load(Movie::class.java, movie.id)
for (actor in matrix.actors) {
println("Actor: " + actor.name)
}
}
}
build.gradle文件如下所示
apply plugin: 'kotlin'
apply plugin: 'application'
apply plugin: "org.jetbrains.kotlin.plugin.noarg"
repositories {
jcenter()
mavenCentral()
maven { url "http://dl.bintray.com/kotlin/ktor" }
maven { url "https://dl.bintray.com/kotlin/kontlinx" }
}
noArg {
annotation("org.neo4j.ogm.annotation.NodeEntity")
annotation("org.neo4j.ogm.annotation.RelationshipEntity")
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "io.ktor:ktor:$ktor_version"
compile "io.ktor:ktor-server-netty:$ktor_version"
compile project(":asoftlibs:micros:administrator:users:users-jvm")
compile 'org.neo4j:neo4j-ogm-core:3.1.2'
compile 'org.neo4j:neo4j-ogm-bolt-driver:3.1.2'
}
kotlin {
experimental {
coroutines "enable"
}
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
sourceCompatibility = "1.8"
我知道类'com.asofttz.micros.administrator.users.testmodels.Movie不是一个有效的实体,如果能得到进一步的帮助,我们将不胜感激。
注意:我也尝试使用no pram构造器打开电影类,但id ddnt也无济于事。另一个尝试是更改ne4j-ogm的版本,因此我测试了2.1.5、3.0.1和3.1.2。没有成功
发布于 2018-08-31 09:16:51
OGM要求类是开放的,并且有一个无参数的构造函数。缺省情况下,Java中的类表现出这些特征,但是在Kotlin中却没有。
您可以将类标记为开放,然后手动添加默认构造函数,也可以使用'no-args‘和'kotlin-spring’gradle插件。这是一个使用Kotlin、Spring Data SDN和OGM的sample application。请注意,在构建文件中我们有:
noArg {
annotation("org.neo4j.ogm.annotation.NodeEntity")
annotation("org.neo4j.ogm.annotation.RelationshipEntity")
annotation("org.springframework.data.neo4j.annotation.QueryResult")
}
这与手动添加默认构造函数相同,但是:
作为替代方案,您可以使用螺栓驱动程序并手动映射查询结果。当您有针对特定用例的自定义查询时,这是一个很好的选择-例如具有高流量的应用程序和仔细调优的查询。
下面的sample application展示了螺栓驱动程序directly的用法。
https://stackoverflow.com/questions/52106841
复制相似问题