关于Corda Persistence并行持久化及vaultQueryBy查询的技术咨询
Let’s walk through each of your questions with clear, practical context:
1. Is my understanding correct that data persists in parallel with the vault (vault remains unchanged, new tables are added)?
Absolutely right. Corda’s vault is the default storage layer for state objects (like ContractState implementations) and maintains its own dedicated database tables (e.g., node_states, node_transactions). When you implement custom persistence using JPA entities (annotated with @Entity), these are stored in entirely separate, new tables that you define.
The vault’s tables never get modified by your custom persistence code—they operate in parallel. You can even link custom entities to vault states using fields like a state’s linearId or externalId if you need to correlate data between the two layers.
2. Does cordaRPCClient.vaultQueryBy automatically choose to query the vault or custom database tables?
Nope—vaultQueryBy is purpose-built for querying the vault only. It’s designed to fetch state objects that Corda manages internally, and it has no awareness of your custom JPA entities or their tables. If you try to use it to fetch data from your custom storage, it won’t work; you’ll need to use separate query logic for those tables.
3. If only part of the data exists in a custom table, how do I determine the data source for queries?
Think of the vault and custom tables as two completely independent data sources:
- Use
vaultQueryBy(or other vault-specific RPC methods likevaultQuery) to fetch data that lives in the vault (i.e., state objects). - For data in custom tables, use JPA’s
EntityManager(you can inject this into flows or retrieve it via Corda’s service APIs) or custom DAO classes to run queries against those tables.
If you need to combine data from both sources, you’ll have to handle the correlation manually in your code. For example, you might fetch a state via vaultQueryBy, then use its linearId to look up a related entity in your custom table using EntityManager.find().
4. Can I explicitly specify Corda to use persistent data for queries?
Yes, but not through vault-specific methods. To query your custom persistent data, you need to interact directly with the JPA layer:
- In flows, you can inject
EntityManagerusing@Injector retrieve it viaserviceHub.jpaEntityManager. - For RPC clients, you can expose custom RPC endpoints in your node that run JPA queries and return the results.
For example, in a flow, you might do something like this:
val myEntity = serviceHub.jpaEntityManager.find(MyCustomEntity::class.java, entityId)
This explicitly targets your custom persistent storage, bypassing the vault entirely.
内容的提问来源于stack exchange,提问作者Ivan

