Search code examples
corda

Corda - Custom Vault Query for Null Column Not Working


Consider the following state:

data class Example(val number: Int?, override val participants: List<AbstractParty>) : QueryableState {

    override fun generateMappedObject(schema: MappedSchema): PersistentState = when (schema) {
        is ExampleEntity.ExampleSchema.ExampleSchemaV1 -> ExampleEntity(number = number)
        else -> throw IllegalArgumentException("Unrecognised schema: $schema.")
    }

    override fun supportedSchemas(): Iterable<MappedSchema> {
        return listOf(ExampleEntity.ExampleSchema.ExampleSchemaV1)
    }
}

Also, consider the following entity:

@Entity
@Table(name = "example_states")
class ExampleEntity(
    @Column(name = "number", nullable = true)
    val number: Int? = null
) : PersistentState() { ... }

In a query, I'm using the following criteria expression:

ExampleEntity::number.equal(null)

This doesn't seem to correctly query for states where the row entries for number are null. Am I doing something wrong?


Solution

  • It appears that null and not null are handled independently. The criteria expression for null would be:

    ExampleEntity::number.isNull()
    

    Therefore additional logic needs to be implemented to perform the query correctly:

    expression = if(number == null) {
        ExampleEntity::number.isNull()
    } else {
        ExampleEntity::number.equal(number)
    }