neo4j-ogm 2.0.0-M02没有保留Scala的嵌套对象

时间:2016-02-24 06:58:15

标签: scala neo4j neo4j-ogm

在JDK 1.7上使用neo4j 2.3.2和neo4j-ogm 2.0.0-M2与Scala 2.11.7

来自build.sbt

val neo4jOgmVersion = "2.0.0-M02"

libraryDependencies += "org.neo4j" % "neo4j-ogm-api" % neo4jOgmVersion
libraryDependencies += "org.neo4j" % "neo4j-ogm-core" % neo4jOgmVersion

我有一个与属性具有一对多关系的实体的简单示例

@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)
      }
    }
  }
}

我可以看到实体附加了属性

Idea Debug Window

保存后我可以看到记录确认成功

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":[]}

但只有实体出现在db

enter image description here

如果我也明确保存属性

session.save(entity)
session.save(attr)

我确认已保存了两个对象

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":[]}

但我最终在db

中有两个孤立的节点

enter image description here

Github Repo Here

关于我绊倒的任何想法?

2 个答案:

答案 0 :(得分:2)

我检查了您的存储库中的代码 - 它失败的原因是因为OGM确实将Scala集识别为集合。

它检查相关实体是否为java.lang.Iterable类型,但您的属性不是。

要验证这是问题所在,我对实体进行了此更改:

@Relationship(`type` = "HAS", direction = "OUTGOING")
var attributes: java.util.Set[Attribute] = new java.util.HashSet()

它按预期工作。

答案 1 :(得分:0)

好的,我想出了如何让链接出现,虽然它对我来说似乎不对。

首先,我从关系中删除了方向

所以来自

@Relationship(`type` = "HAS", direction = "INCOMING")

@Relationship(`type` = "HAS")

然后修改main以使属性链接回实体并保存属性

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
    attr.entity = entity

    try {
      session.save(entity)
      session.save(attr)
      tx.commit()
    } catch {
      case e: Exception => {
        println(e)
      }
    }
  }
}