How to query on a field in nested collection of a parent state using VaultCustomQuery

[亡魂溺海] 提交于 2019-12-02 08:20:55

Your schema definition is correct (and you can see another example here: Querying nested collections in LinearState states).

However, querying nested collections is not supported by VaultCustomQueryCriteria. You have to do direct JDBC queries to query attributes of the nested collections.

Here is an example of a direct JDBC query in Corda:

@Test
fun `test calling an arbitrary JDBC native query`() {
    val nativeQuery = "SELECT v.transaction_id, v.output_index FROM vault_states v WHERE v.state_status = 0"

    database.transaction {
        val jdbcSession = services.jdbcSession()
        val prepStatement = jdbcSession.prepareStatement(nativeQuery)
        val rs = prepStatement.executeQuery()
        var count = 0
        while (rs.next()) {
            val stateRef = StateRef(SecureHash.parse(rs.getString(1)), rs.getInt(2))
            Assert.assertTrue(cashStates.map { it.ref }.contains(stateRef))
            count++
        }
        Assert.assertEquals(cashStates.count(), count)
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!