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

后端 未结 1 1552
长发绾君心
长发绾君心 2021-01-16 12:45

I have a one-to-many relationship where I am trying to add a list of object/class in my state. i.e I have a contract state that has a list of attachments List

相关标签:
1条回答
  • 2021-01-16 13:05

    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)
        }
    }
    
    0 讨论(0)
提交回复
热议问题