JDK 1.7 上の Scala 2.11.7 で neo4j-ogm 2.0.0-M2 で neo4j 2.3.2 を使用する
build.sbt から
val neo4jOgmVersion = "2.0.0-M02"
libraryDependencies += "org.neo4j" % "neo4j-ogm-api" % neo4jOgmVersion
libraryDependencies += "org.neo4j" % "neo4j-ogm-core" % neo4jOgmVersion
属性と1対多の関係を持つエンティティの簡単な例があります
@NodeEntity
class Entity {
@GraphId
@BeanProperty
var id: java.lang.Long = _
@BeanProperty
var name: String = _
@BeanProperty
var sourceId: String = _
@Relationship(`type` = "HAS", direction = "OUTGOING")
var attributes: Set[Attribute] = Set()
def this(name: String, sourceId: String) {
this()
this.name = name
this.sourceId = sourceId
}
}
それに応じて、属性は次のようになります
@NodeEntity
class Attribute {
@GraphId
@BeanProperty
var id: java.lang.Long = _
@BeanProperty
var name: String = _
@BeanProperty
var `type`: String = _
@BeanProperty
var value: String = _
@Relationship(`type` = "HAS", direction = "INCOMING")
var entity: Entity = _
def this(name: String, `type`: String, value: String) {
this()
this.name = name
this.`type` = `type`
this.value = value
}
}
関係も定義しました
@RelationshipEntity(`type` = "HAS")
class Has {
@GraphId
@BeanProperty
var id: java.lang.Long = _
@StartNode
var entity: Entity = _
@EndNode
var attribute: Attribute = _
def this(entity: Entity, attribute: Attribute) {
this()
this.entity = entity
this.attribute = attribute
}
}
簡単な例の実行
object Main {
def main(args: Array[String]): Unit = {
val session = Neo4jSessionFactory.getNeo4jSession()
val tx: transaction.Transaction = session.beginTransaction()
val entity = new Entity("EntityName","external_ref")
val attr = new Attribute("Attr1","attr_type","AttrVal")
entity.attributes += attr
try {
session.save(entity)
tx.commit()
} catch {
case e: Exception => {
println(e)
}
}
}
}
エンティティにアタッチされた属性があることがわかります
保存すると、ロギングが成功したことを確認できます
06:49:47.185 [main] INFO o.n.o.d.http.request.HttpRequest - POST http://localhost:7474/db/data/transaction/302, request {"statements":[{"statement":"UNWIND {rows} as row CREATE (n:`Entity`) SET n=row.props RETURN row.nodeRef as nodeRef, ID(n) as nodeId","parameters":{"rows":[{"nodeRef":-2090479386,"props":{"name":"EntityName","sourceId":"external_ref"}}]},"resultDataContents":["row"],"includeStats":false}]}
06:49:47.198 [main] DEBUG o.n.o.d.http.request.HttpRequest - Response is OK
06:49:47.309 [main] DEBUG o.n.o.d.http.request.HttpRequest - Response is OK
06:49:47.310 [main] DEBUG o.n.o.drivers.http.driver.HttpDriver - {"results":[],"errors":[]}
しかし、データベースにはエンティティのみが表示されます
属性も明示的に保存すると
session.save(entity)
session.save(attr)
2 つのオブジェクトが保存されたことが確認されました
06:54:08.658 [main] INFO o.n.o.d.http.request.HttpRequest - POST http://localhost:7474/db/data/transaction/307, request {"statements":[{"statement":"UNWIND {rows} as row CREATE (n:`Entity`) SET n=row.props RETURN row.nodeRef as nodeRef, ID(n) as nodeId","parameters":{"rows":[{"nodeRef":-1747010532,"props":{"name":"EntityName","sourceId":"external_ref"}}]},"resultDataContents":["row"],"includeStats":false}]}
06:54:08.669 [main] DEBUG o.n.o.d.http.request.HttpRequest - Response is OK
06:54:08.746 [main] DEBUG o.n.ogm.context.EntityGraphMapper - context initialised with 0 relationships
06:54:08.747 [main] DEBUG o.n.ogm.context.EntityGraphMapper - visiting: domain.Attribute@174f19bc
06:54:08.747 [main] DEBUG o.n.ogm.context.EntityGraphMapper - domain.Attribute@174f19bc has changed
06:54:08.747 [main] DEBUG o.n.ogm.context.EntityGraphMapper - mapping references declared by: domain.Attribute@174f19bc
06:54:08.748 [main] DEBUG o.n.o.drivers.http.driver.HttpDriver - request url http://localhost:7474/db/data/transaction/307
06:54:08.748 [main] INFO o.n.o.d.http.request.HttpRequest - POST http://localhost:7474/db/data/transaction/307, request {"statements":[{"statement":"UNWIND {rows} as row CREATE (n:`Attribute`) SET n=row.props RETURN row.nodeRef as nodeRef, ID(n) as nodeId","parameters":{"rows":[{"nodeRef":-391059900,"props":{"name":"Attr1","value":"AttrVal","type":"attr_type"}}]},"resultDataContents":["row"],"includeStats":false}]}
06:54:08.757 [main] DEBUG o.n.o.d.http.request.HttpRequest - Response is OK
06:54:08.772 [main] DEBUG o.n.o.d.http.request.HttpRequest - Response is OK
06:54:08.772 [main] DEBUG o.n.o.drivers.http.driver.HttpDriver - {"results":[],"errors":[]}
しかし、データベースに2つの孤立したノードができてしまいます
私がつまずいている場所についてのアイデアはありますか?