Search code examples
mongodbscalaplayframeworkjacksonjongo

Play 2.3 Jongo ObjectId to custom ID


I use Play 2.3 scala with play-jongo:

"uk.co.panaxiom" %% "play-jongo" % "0.7.1-jongo1.0"

I have scala case class:

import org.jongo.marshall.jackson.oid.Id
case class User(@Id id: String, name: String)

When i save some user using Jongo PlayJongo.getCollection("users").save(User("uuid","user name"))

I have following in my DB:

{ 
    "_id" : { "$oid" : "5612a4effa93746a877c0d5c"} , 
    "id" : "uuid", 
    "name": "user name"
}

When required result would be with string valued _id which maps to id attribute in scala class:

{ 
    "_id" : "uuid", 
    "name": "user name"
}

Everything works OK when i turn my scala case class to Java POJO (simple example below). In this case i get required result.

public class User {
    @Id
    public String id;
    public String name;
}

What am I doing wrong? How can I make jackson/jongo understand that @Id annotation correctly?

In other SO answers there are suggestions to use @Id annotation - which I use - without success.


Solution

  • So the RTFM rule pays off after all. It's stated at play-jongo readme that it's enough to add Jackson Module Scala dependency to the project and configure mapper.

    Add dependency to build.sbt:

    "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.6.1",
    

    Configure mapper in application.conf

    playjongo.mapperfactory="uk.co.panaxiom.playjongo.JongoScalaMapperFactory"