如何注册自定义架构并使其在Corda中可查询? [英] how to register Custom Schema and make it queryable in Corda?

查看:81
本文介绍了如何注册自定义架构并使其在Corda中可查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关问题(无法帮助我解决问题):

related issues (that didn't help me resolve the problem):

如何创建自定义架构使用Corda Vault扩展程序

从Corda Custom获取数据时出错表格

和其他一些: https://stackoverflow.com/search?q=%5Bcorda%5D + custom + schema

我遇到一个与自定义架构注册"有关的错误(我认为).当使用状态架构中字段的条件运行VaultQuery时,出现以下异常:

I'm experiencing an error, related to Custom Schema Registration (I think). When running a VaultQuery with criteria for the fields in my state schema, I get the following exception:

org.hibernate.InstantiationException:实体的默认构造函数:com.template.schema.SharedItemSchemaV1 $ PersistentSharedItem

org.hibernate.InstantiationException: No default constructor for entity: : com.template.schema.SharedItemSchemaV1$PersistentSharedItem

如果我添加一个默认的构造函数,则:

and if I add a default constructor, then:

vault.NodeVaultService._queryBy-org.hibernate.InstantiationException:无法实例化实体:: com.template.schema.SharedItemSchemaV1 $ PersistentSharedItem

vault.NodeVaultService._queryBy - org.hibernate.InstantiationException: Could not instantiate entity: : com.template.schema.SharedItemSchemaV1$PersistentSharedItem

项目在这里: https://github.com/tradle/sharing-cordapp

如果您运行流测试,则它们应该通过.如果您替换此行( https使用以下代码//://github.com/tradle/sharing-cordapp/blob/master/cordapp/src/main/java/com/template/ResolveToIdentityFlow.java#L87 ),然后重新运行Flow测试,您将遇到我提到的异常.

If you run the Flow Tests, they should pass. If you replace this line (https://github.com/tradle/sharing-cordapp/blob/master/cordapp/src/main/java/com/template/ResolveToIdentityFlow.java#L87) with the code below, and re-run the Flow Tests, you'll hit the exception I mentioned.

QueryCriteria条件=新的VaultQueryCriteria(Vault.StateStatus.UNCONSUMED).and(新VaultCustomQueryCriteria(tmpIdCriteria));

QueryCriteria criteria = new VaultQueryCriteria(Vault.StateStatus.UNCONSUMED) .and(new VaultCustomQueryCriteria(tmpIdCriteria));

推荐答案

使用默认构造函数似乎存在问题,该默认构造函数为每个值传递 null .使用以下内容对我有用:

There appears to be an issue with using a default constructor which passes null for every value. Using the following instead worked for me:

public PersistentSharedItem() {
    this("", "", "", "", 0L, UUID.randomUUID());
}

此外,在使用自定义文件库查询时,您必须始终引用实体属性而不是状态属性,因为所有查询都使用在我们的 NodeSchemaService 中注册并可用的Hibernate实体.所以不要这样做:

Additionally, when using custom vault queries, you must always reference the entity attributes rather than the state attributes, as all queries use the Hibernate entities which are registered and usable with our NodeSchemaService. So instead of doing:

linkField = SharedItemState.class.getDeclaredField("link");

您需要这样做:

linkField = SharedItemSchemaV1.PersistentSharedItem.class.getDeclaredField("link");

如果要在流测试中使用架构,则还需要在每个模拟节点上注册自定义架构:

If you want to use your schemas in flow tests, you also need to register the custom schema on each mock node:

for (StartedNode<MockNetwork.MockNode> node : nodes.getPartyNodes()) {
    node.getInternals().registerCustomSchemas(
        ImmutableSet.of(new SharedItemSchemaV1())
    );
}

将来,自定义模式的注册很可能会自动进行.

In the future, the registration of custom schemas is this way is likely to become automatic.

这篇关于如何注册自定义架构并使其在Corda中可查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆