DataStax Academy FAQ

DataStax Academy migrated to a new learning management system (LMS) in July 2020. We are also moving to a new Cassandra Certification process so there are changes to exam bookings, voucher system and issuing of certificates.

Check out the Academy FAQ pages for answers to your questions:


question

Venky.kv_181838 avatar image
Venky.kv_181838 asked ·

java-driver-mapper-processor throwing error when using kotlin suspend function for dao methods like @insert, @select

The following error is thrown by KaptPlugin


Invalid return type: Select methods must return one of [ENTITY, OPTIONAL_ENTITY, FUTURE_OF_ENTITY, FUTURE_OF_OPTIONAL_ENTITY, PAGING_ITERABLE, FUTURE_OF_ASYNC_PAGING_ITERABLE]@Select


I have the following function signature:

@Dao
interface MyDao {
    @Select
    suspend fun get(id: UUID): Product
}


Does the mapper generator support kotlin suspend return type? if no, are there any workarounds?

mapperkaptkotlin
1 comment
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Just acknowledging your question. I'm in transit so I'll see if I can get someone to respond.

0 Likes 0 · ·
olivier.michallat_30685 avatar image
olivier.michallat_30685 answered ·

Hi,

No, the mapper doesn't support Kotlin native features, such as suspended functions.

I'm not a Kotlin expert, but from what I can see online it looks like you can bridge the gap between JDK8 futures and coroutines using the kotlin-coroutines-jdk8 library. So if you have your DAO method return a CompletionStage<Product> instead, you can wrap it like this:

suspend fun get(id: UUID): Product {
  return myDao.get(id).await
}

Where you put this other method is a matter of taste. It could be a default method on the DAO interface itself, but then the CompletionStage method is also visible at the same level. Or you could write a wrapper class, but then it's a bit more cumbersome if you need to wrap every DAO manually. Or Kotlin might have a more elegant way of handling this that I'm not aware of.

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Erick Ramirez avatar image
Erick Ramirez answered ·

@Venky.kv_181838 Just wanted to let you know that I haven't forgotten about your question. I don't have experience with Kotlin and I'm looking for confirmation that coroutines are not supported currently by the Java driver. We don't have any workarounds available for it at the moment.

I will update my response as soon as I have more info. Otherwise, someone from our Drivers team will post a response too. Cheers!

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Cedrick Lunven avatar image
Cedrick Lunven answered ·

Also @Venky.kv_181838


I would be interested to have an sample code on Kotlin leveraging on the latester driver version if you do have portion of your code than you can share It would help the community here I guess.


Best Regards

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.