Corda 4.7中LinearStateQueryCriteria与QueryCriteria类型不兼容导致的编译错误求助
It sounds like you're hitting a common API version mismatch issue with Corda's vault query classes—let's break this down and fix it step by step.
Root Cause
Corda has adjusted the structure of its QueryCriteria classes across versions:
- In older Corda versions (pre-4.5 roughly),
LinearStateQueryCriteriawas a top-level class directly undernet.corda.core.node.services.vault. - In newer versions,
LinearStateQueryCriteriawas moved to be an inner class ofQueryCriteria(so it'sQueryCriteria.LinearStateQueryCriteria).
The confusion happens when the documentation you're referencing is for a different version than the one you're using, leading to mismatched class structures and method signatures.
Fix 1: Align Your Code with Corda Version
If you're using an older Corda version (pre-4.5):
Ensure you're importing the top-level LinearStateQueryCriteria and using it directly with queryBy() (making sure to specify your state class as the first parameter):
import net.corda.core.node.services.vault.LinearStateQueryCriteria; import net.corda.core.node.services.vault.Vault; import com.google.common.collect.ImmutableList; // Your linear ID string String linearId = "your-linear-id-here"; // Build query criteria LinearStateQueryCriteria queryCriteria = new LinearStateQueryCriteria( null, ImmutableList.of(UUID.fromString(linearId)) ); // Execute query—don't forget to pass your state class! Vault.Page<YourLinearState> results = getServiceHub().getVaultService().queryBy(YourLinearState.class, queryCriteria);
If you're using a newer Corda version (4.5+):
Use the inner QueryCriteria.LinearStateQueryCriteria class, again ensuring you specify your state class when calling queryBy():
import net.corda.core.node.services.vault.QueryCriteria; import net.corda.core.node.services.vault.Vault; import com.google.common.collect.ImmutableList; // Your linear ID string String linearId = "your-linear-id-here"; // Build query criteria using the inner class QueryCriteria.LinearStateQueryCriteria queryCriteria = new QueryCriteria.LinearStateQueryCriteria( null, ImmutableList.of(UUID.fromString(linearId)) ); // Execute query with your state class Vault.Page<YourLinearState> results = getServiceHub().getVaultService().queryBy(YourLinearState.class, queryCriteria);
Fix 2: Verify queryBy() Method Signature
The most common mistake here is forgetting to pass your linear state class as the first parameter to queryBy(). The method requires both the state type and the criteria—without the state class, the compiler won't resolve the correct overload, leading to the "cannot accept queryCriteria" error.
Additional Checks
- Double-check your
build.gradlefile to confirm your Corda version matches the documentation you're using. - Ensure your imports don't mix old and new classes (e.g., don't import both
net.corda.core.node.services.vault.LinearStateQueryCriteriaandnet.corda.core.node.services.vault.QueryCriteriaif you're on a new version).
内容的提问来源于stack exchange,提问作者Sourabh

