You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Hyperledger Composer查询assetStatus为invalid的资产所属Owner?

Querying Owners for Assets with "invalid" Status in Hyperledger Composer

Hey there, let's work through how to pull the Owners linked to assets marked as "invalid" using Hyperledger Composer. First, let's recap your model definition for context:

namespace org.acme.myNetwork
asset Asset identified by assetId {
  o String assetId
  --> Owner owner
  o String assetStatus
}
participant Owner identified by ownerId {
  o String ownerId
  o String ownerName
  o String ownerStatus
}

You're right that basic single-condition examples are common in docs, but Composer's query language absolutely supports the kind of关联 query you need. Here are two straightforward ways to make this work:


Option 1: Directly Query for Owners via Linked Invalid Assets

You can craft a query that targets Owner participants, filtering only those who have at least one asset with an "invalid" status. The exists keyword here is perfect for checking if a matching linked asset exists:

query getOwnersOfInvalidAssets {
  description: "Fetch all Owners associated with assets that have status 'invalid'"
  participant org.acme.myNetwork.Owner
  where (
    exists (
      SELECT org.acme.myNetwork.Asset 
      WHERE (assetStatus == "invalid" AND owner == this)
    )
  )
}

How this works:

  • this refers to the current Owner instance being evaluated in the query
  • exists checks if there's any Asset linked to that Owner where assetStatus is "invalid"
  • The query returns only those Owners that meet this criteria

Option 2: First Fetch Invalid Assets, Then Extract Owners

If you prefer a two-step approach (maybe you need the invalid assets for other purposes too), you can first query the assets, then pull their linked Owners in your application code:

Step 1: Query for Invalid Assets

First, create a query to get all assets with the "invalid" status:

query getInvalidAssets {
  description: "Get all assets marked as 'invalid'"
  asset org.acme.myNetwork.Asset
  where (assetStatus == "invalid")
}

Step 2: Extract Owners in Your Code (JavaScript Example)

Once you have the invalid assets, map over them to get their linked Owners. We'll also add a step to deduplicate in case one Owner has multiple invalid assets:

// Fetch the invalid assets from the network
const invalidAssets = await businessNetworkConnection.query('getInvalidAssets');

// Extract the linked Owner from each asset
const owners = invalidAssets.map(asset => asset.owner);

// Remove duplicate Owners (if needed)
const uniqueOwners = [...new Set(owners)];

Quick Tips

  • If you want to make the query flexible (e.g., pass in different status values dynamically), replace "invalid" with a parameter like _$status, then pass the value when you call the query
  • Always test your queries using the Composer Playground first—it's a great way to verify results before integrating into your app

内容的提问来源于stack exchange,提问作者Keanu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 07:04:16