Search code examples
scalaneo4jneo4j-ogm

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


Using neo4j 2.3.2 with neo4j-ogm 2.0.0-M2 with Scala 2.11.7 on JDK 1.7

From build.sbt

val neo4jOgmVersion = "2.0.0-M02"

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

I have a simple example of an Entity that has a one to many relationship with attributes

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

Correspondingly the Attribute is like this

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

I have also defined the relationship

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

Running a simple example

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

I can see that the Entity has the attribute attached

Idea Debug Window

And upon saving I can see the logging confirms a success

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

But only the entity appears in the db

enter image description here

If I explicitly save the Attribute as well

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

I get confirmation that two objects have been saved

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

But I end up with two isolated nodes in the db

enter image description here

Github Repo Here

Any ideas on where I'm tripping up?


Solution

  • I checked the code in your repository- the reason it fails is because the OGM does recognise a Scala Set as a collection.

    It checks if the related entity is of type java.lang.Iterable but your attributes are not.

    To verify that this was the issue, I made this change to Entity:

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

    and it works as expected.