neo4j-ogm 2.0.0-M02 不保留来自 Scala 的嵌套对象

neo4j-ogm 2.0.0-M02 not persisting nested object from Scala

在 JDK 1.7

上使用带有 Scala 2.11.7 的 neo4j 2.3.2 和 neo4j-ogm 2.0.0-M2

来自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
  }
}

对应的Attribute是这样的

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

我确认两个对象已保存

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

但我最终在数据库中得到了两个孤立的节点

Github Repo Here

关于我在哪里绊倒的任何想法?

好的,我想出了如何让 link 出现的方法,尽管它对我来说似乎不正确。

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

所以从

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

@Relationship(`type` = "HAS")

然后修改 main 以便属性 link返回实体并保存属性

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

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

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

为了验证这是问题所在,我对实体进行了以下更改:

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

它按预期工作。