How to fetch data from custom table in Corda

253 Views Asked by At

We are trying to fetch data from the our custom tables using the vault query with state as query criteria. But we get a state with a model class which contains the old values and not the new ones from the database. Can someone point out to an example of fetching the data from a custom table and mapping it to the model class in corda.

1

There are 1 best solutions below

0
On

Mapping from the schema to state's properties can be provided in the state class by overriding the method generateMappedObject from the QueryableState class. Make sure your state implements QueryableState class.

@Override public PersistentState generateMappedObject(MappedSchema schema) {
        if (schema instanceof IOUSchemaV1) {
            return new IOUSchemaV1.PersistentIOU(
                    this.lender.getName().toString(),
                    this.borrower.getName().toString(),
                    this.value,
                    this.linearId.getId());
        } else {
            throw new IllegalArgumentException("Unrecognised schema $schema");
        }
    }

You can then query the database using vault query like

QueryCriteria queryCriteria = new QueryCriteria.VaultQueryCriteria().withStatus(Vault.StateStatus.UNCONSUMED);

List<StateAndRef<IOUState>> iouStates = getServiceHub().getVaultService()
                .queryBy(IOUState, queryCriteria).getStates();