Search code examples
scalaneo4jneo4j-ogm

Neo4J OGM field primary id is null in Scala


This is my node entity class:

@NodeEntity
class SutStateEntity(
    @Id
    @GeneratedValue
    @Index(unique = true)
    val id: String) {

  def this(sutStateIdentifier: SutStateIdentifier) = this(sutStateIdentifier.hash)
  def this() = this("")
}

I am using the unique hash value of sutStateIdentifier as my id. When I store the a SutStateEntity inside of a transaction:

val sutStateEntity = new SutStateEntity(sutStateIdentifier)
session.save(sutStateEntity)

I get the following exception:

Exception in thread "main" org.neo4j.ogm.exception.core.MappingException: `Field with primary id is null for entity ....SutStateEntity@34aa8b61`

I have read that this error occurs if I have not specified a default constructor which I did.

edit: The following example works:

@Id
@GeneratedValue
var id: java.lang.Long = 0L

I guess, I have to change the field id to var but it still does not work if I use a String. Not even with a java.lang.String.


Solution

  • The assigment is wrong. This works:

    @Id
    @GeneratedValue
    var id: java.lang.Long