hibernate - 将子类按自定义FungibleState模式的瞬时父属性分组

标签 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保留在子类上,但是这导致在数据库中存储重复的值。

最佳答案

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

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,因此其列将映射到子实体中。

关于hibernate - 将子类按自定义FungibleState模式的瞬时父属性分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49511406/

相关文章:

android - 是否有可能将 try catch 函数作为中缀函数?

database - 对方在意外时间发送 session 拒绝消息,消息无法建立 session

java - 任务“:workflows-java:NodeDriver. main()”执行失败

java - Spring3, hibernate ;我如何使用 HibernateTemplate

java - 用于对数据库进行复杂查询的 Hibernate Criteria Class

android - 403 禁止。我们计算的请求签名与您提供的签名不匹配。检查从 Android 到 Amazon S3 的 key 和签名方法

corda - proxies.ExceptionSerialisingRpcOpsProxy.log - RPC 调用期间出错 [errorCode=19t75of]

hibernate - Lucene:查找以特定前缀开头的所有单词

java - hibernate 可选择不自动递增 ID 字段

java - 如何设计一个包含一些类的复杂类,以便将来在 Kotlin 中更容易扩展?