Retrieving a single Int value from the database with Kotlin

Hi there.

I’m using the new Kotlin stuff in 7.2, and I’ve hit on a snag.

I have a piece of JPQL to retrieve the highest value in a table field:

        //language=EQL
        const val GET_HIGHEST_SEQUENCE_NUMBER_JPQL = 
"""select max(t.sequenceNumber) 
               from kipper_TaskTemplate t 
                where t.processTemplate = :processTemplate"""

t.sequenceNumber should be an integer value, but I’m not sure how to retrieve it in Kotlin:

 val highestSequenceNumber =  dataManager.loadValue(GET_HIGHEST_SEQUENCE_NUMBER_JPQL, Int::class)
                .parameter("processTemplate", processTemplate)
                .one()

The second parameter for the loadValue call – what should that be if I’m returning an Int?

Most likely you need Int? as all database results are nullable and then java.lang.Integer::class.java should be used.

1 Like

Yep, that was pretty much it. Thanks. In the end I went with:

        val highestSequenceNumber: Optional<Int> = dataManager.loadValue(GET_HIGHEST_SEQUENCE_NUMBER_JPQL, Int::class.java)
                .parameter("processTemplate", processTemplate)
                .optional()

There could be Kotlin extension function to simplify this to Int::class and Int? instead of Optional<Int>. May be the team will consider introducing something like this.