【问题标题】:Group a child class on transient parent attributes for a custom FungibleState schema在自定义 FungibleState 模式的瞬态父属性上对子类进行分组
【发布时间】:2018-09-05 18:36:18
【问题描述】:

在 Corda V3 中,我们正在扩展 FungibleState,如 API Vault Query 文档中所述:

object CustomSchemaV1 : MappedSchema(schemaFamily = CustomSchema.javaClass, version = 1, mappedTypes = listOf(PersistentCustomState::class.java))
{
@Entity
@Table(name = "custom_states", indexes = arrayOf(Index(name = "custom_field_idx", columnList = "custom_field")))
class PersistentCustomState(

/** Custom attributes */
@Column(name = "custom_field")
var customField: String? = null,

/** FungibleState parent attributes */
@Transient
val _participants: Set<AbstractParty>,
@Transient
val _owner: AbstractParty,
@Transient
val _quantity: Long,
@Transient
val _issuerParty: AbstractParty,
@Transient
val _issuerRef: OpaqueBytes
) : CommonSchemaV1.FungibleState(_participants?.toMutableSet(), _owner, _quantity, _issuerParty, _issuerRef.bytes)}

使用这种模式,我们如何对 _quantity 父字段进行求和,按 custom_field 分组?

查看示例,我们尝试使用QueryCriteria

val sum = builder {
    CustomSchemaV1.PersistentCustomState::_quantity.sum(
        groupByColumns = listOf(
            CustomSchemaV1.PersistentCustomState::customField
        ),
        orderBy = Sort.Direction.ASC
    )
}
return QueryCriteria.VaultCustomQueryCriteria(sum)

但这会引发错误:Unable to locate Attribute with the the given name [_quantity] on this ManagedType [net.corda.core.schemas.PersistentState]

我们能够解决删除 @Transient 注释的方法,该注释也将 quantity 保留在子类上,但这会导致在数据库中存储重复值。

【问题讨论】:

    标签: hibernate kotlin corda


    【解决方案1】:

    您是否考虑过尝试以下方法:

    object CustomSchemaV1 : MappedSchema(schemaFamily = CustomSchema.javaClass, version = 1, mappedTypes = listOf(PersistentCustomState::class.java))
    {
    @Entity
    @Table(name = "custom_states", indexes = arrayOf(Index(name = "custom_field_idx", columnList = "custom_field")))
    class PersistentCustomState(
    
    /** Custom attributes */
    @Column(name = "custom_field")
    var customField: String? = null,
    
    /** FungibleState parent attributes */
    participants: MutableSet<AbstractParty>?,
    owner: AbstractParty,
    quantity: Long,
    issuerParty: AbstractParty,
    issuerRef: OpaqueBytes
    ) : CommonSchemaV1.FungibleState(participants, owner, quantity, issuerParty, issuerRef.bytes)}
    

    然后使用继承的quantity 列? CommonSchemaV1.FungibleState 是一个MappedSuperclass,所以它的列会被映射到子实体中。

    【讨论】:

    • 尝试了这种方法,但现在我在 CommonSchemaV1 上遇到错误:Please register the entity 'net.corda.core.schemas.CommonSchemaV1' 这很奇怪,因为这显然不是自定义模式。运行 NodeDriver 或使用 cordapp jar 时出现同样的错误。
    • 嗯,这很有趣。这看起来可能是 Corda 中的一个错误,我们需要在这里更深入地挖掘。我们已经设法使用来自finance 模块自己的单元测试的SampleCashSchemaV2 重现了这个问题。
    【解决方案2】:

    这是一个正在此处跟踪的错误:https://r3-cev.atlassian.net/browse/CORDA-1338

    【讨论】:

      猜你喜欢
      • 2013-03-04
      • 1970-01-01
      • 2015-10-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 2014-06-26
      • 2016-02-14
      • 1970-01-01
      相关资源
      最近更新 更多