Search code examples
spring-data-neo4jneo4j-ogm

How to have non generated Id's in mapped entities


I am trying to import a bunch of record's from RDBMS and create connected nodes in neo4j using Spring Data Neo4j because would like to do this periodically and by doing this sync the Neo4j database with the one in the master RDBMS.

My problem at the moment is that I need the id's in coming to be kept as is so that I am able to link the subsequent records in different node types, but if I populate the id's with the original values the the nodes are not created in the Neo4j. One example entity that I have is:

@NodeEntity
data class CarAgent(
        @Id 
        override var id: Long? = null,
        var name: String = "",
        var country: String = "",
        var desc: String = "",
        var status: Boolean = false,
        var xmlType: String = "",
        var company: String) : Entity

And the import logic is doing:

@Suppress("UNCHECKED_CAST")
fun processRow(exchange: Exchange) {
    val message = exchange.`in`.body as List<String>
    val id = message[0]
    val name = message[1]
    val country = message[4]
    val desc = message[19]
    val status = message[20]
    val xml = message[25]
    val company = message[31]
    val carAgent = CarAgent(
            id = id.toLong(),
            name = name,
            country = country,
            desc = desc,
            status = status.toBoolean(),
            xmlType = xml,
            company = company)
    carAgentDataRepository.save(carAgent)
}

It only saves the entity if I add the annotation @GeneratedValue to the Id field.


Solution

  • In entities, a Long id field is considered as special by OGM/SDN. It is mapped to the database internal node id. This behavior is inherited from previous OGM versions.

    Try changing the name of the field to something like legacyId and it should work.