native POJO codec not recognized

137 views
Skip to first unread message

Vincent Lee

unread,
Sep 12, 2020, 9:44:48 PM9/12/20
to kmongo
I can't get the mapping to work using native or jackson and could use some guidance.  

build.gradle.kts
// plugins 
kotlin("jvm") version "1.4.10"
// dependencies
implementation("org.litote.kmongo:kmongo-native:4.1.2")

db init
val client = KMongo.createClient() 
codecRegistries = CodecRegistries.fromRegistries( // save uuids as UUID, instead of LUUID
CodecRegistries.fromProviders(
PojoCodecProvider
.builder()
.automatic(true)
.build()),
MongoClientSettings.getDefaultCodecRegistry()
)
mongoConnection = client.getDatabase("property").withCodecRegistry(codecRegistries())

usage 
data class UserEntity @BsonCreator constructor(
@BsonId val _id: String,
@BsonProperty("name") val name: String,
@BsonProperty("email") val email: String)

database.getMongoConnection()
.getCollection<UserEntity>()
.find()
.toList()
// errors here

[qtp60559178-18] WARN org.bson.codecs.pojo - Cannot use 'UserEntity' with the PojoCodec.
java.lang.IllegalArgumentException: propertyName can not be null

Connection to db and everything else is fine.

Thanks in advance!



zigzago

unread,
Sep 13, 2020, 11:01:23 AM9/13/20
to kmongo
Hello,

Your code removes the KMongo native codec registry. Just insert the custom codec like this:

val mongoConnection = client.getDatabase("property").run {
     withCodecRegistry(
         CodecRegistries.fromProviders(
            //your custom codec
            UuidCodecProvider(/*chose the UuidRepresentation you need*/),
           //default codec registry with kmongo codec registry set by KMongo
            codecRegistry
)
}

HTH

Vincent Lee

unread,
Sep 13, 2020, 2:29:44 PM9/13/20
to kmongo
Ok, I've followed the instructions but I'm still getting the same error 

Can't find a codec for class propertymanager.modules.users.UserEntity.

Entity definition
@Entity // no-arg constructor 
data class UserEntity(@BsonId val _id: UUID, val name: String, val email: String)

Database init
val client = KMongo.createClient()
val database = client.getDatabase("property")
.run {
    withCodecRegistry(
        CodecRegistries.fromRegistries(
            CodecRegistries.fromProviders(
                UuidCodecProvider(UuidRepresentation.STANDARD)),
                MongoClientSettings.getDefaultCodecRegistry())) 
}

Query
private val collection = database.getMongoConnection().getCollection<UserEntity>("users")
collection.find().toList() // Can't find a codec for class UserEntity.

Vincent Lee

unread,
Sep 13, 2020, 2:36:07 PM9/13/20
to kmongo
fixed formatting 

val client = KMongo.createClient()
val database = client.getDatabase("property")
.run {
    withCodecRegistry(
        CodecRegistries.fromRegistries(
            CodecRegistries.fromProviders(UuidCodecProvider(UuidRepresentation.STANDARD),MongoClientSettings.getDefaultCodecRegistry())
        )
   )
}

zigzago

unread,
Sep 13, 2020, 3:42:21 PM9/13/20
to kmongo
Do not use MongoClientSettings.getDefaultCodecRegistry(), use this.getCodecRegistry(). MongoClientSettings.getDefaultCodecRegistry() does not contain KMongo codec registry.

Vincent Lee

unread,
Sep 14, 2020, 12:17:38 AM9/14/20
to kmongo
ugh that's clear now. thank you!
Reply all
Reply to author
Forward
0 new messages