Search code examples
scalaneo4jneo4j-ogm

Labels in Neo4j OGM with Scala


I am trying to implement multi tenancy in Neo4j using OGM. My favourite solution is using labels to label entities with tenants ID. Unfortunately I can not get labels to work in Scala. First, I created my entities like this:

trait Entity {
  @GraphId
  @Property
  var graphId: java.lang.Long = _
}

@NodeEntity
class Tenant extends Entity {

   @Property
   var name: String = _

}

@NodeEntity
class Project extends Entity {

   @Property
   var name: String = _

   @Relationship(`type` = "TENANT", direction = Relationship.OUTGOING)
   var tenant: Tenant = _

   @Labels
   val labels: java.util.Collection[String] = new java.util.ArrayList[String]()

   def this(name: String, tenant: Tenant) {
    this()
    this.labels.add(tanant.uuid.toString)

   }

   def hasLabel(label: String): Boolean = {
     this.labels.contains(label)
   }

}

My repositories just have a simple save method

abstract class Repository[Model <: Entity] {

  def save(entity: Model): Model = {
    val session = getSession()
    session.save(entity)

    entity
  }

}

I created a simple test with an embedded Neo4j database, but the last assertion fails with false (what means that the added label is not persisted).

class ProjectSuite extends BaseSuite with Neo4jOgmTestSuite {

  "Project Model" should {

    "save properly with Tenant label" in {
      val tenant = new Tenant("TestTenant")
      TanantRepository.save(tenant)

      val project = new Project("TestProject", tenant)
      ProjectRepository.save(project)

      assert(project.hasLabel(tenant.uuid.toString))

      val loadedProject = ProjectRepository.findById(project.uuid)
      assert(loadedProject isDefined)
      assert(loadedProject.get.hasLabel(tenant.uuid.toString))
    }

  }

}

Any idea what I am missing here?


Solution

  • This issue was a bug in that any class without a get/set method backing the labels field was not supported. It has been fixed, and is available in 2.1.0-SNAPSHOT.