在自定义FungibleState模式的瞬态父属性上对子类进行分组

时间:2018-03-27 11:11:58

标签: hibernate kotlin corda

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

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,但这会导致在数据库中存储重复的值。

2 个答案:

答案 0 :(得分: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.FungibleStateMappedSuperclass,因此其列将映射到子实体。

答案 1 :(得分:0)

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