具有自定义数据类型的集合的持久模式映射

时间:2018-07-08 03:36:39

标签: corda

我有以下情况。 我有一个线性状态,如下所示。

class INDENTState(
                  val indentId:String,
                  val itemType: String,
                  val model: String,
                  val quantity: Int,
                  val specifications:String,
                  val product : String,
                  val comment:String,
                  val branchName:String,
                  val branchAddress:String,
                  val state:String,

          override val linearId: UniqueIdentifier = UniqueIdentifier(indentId)):
        LinearState, QueryableState {

我想将上述线性状态包含在另一个linearState中,并将几个INDENTState插入列表中,然后创建另一个线性状态。

我想保持第二线性状态。

object CollatedIndentsSchemaV1 : MappedSchema(
        schemaFamily = CollatedIndentsSchema.javaClass,
        version = 1,
        mappedTypes = listOf(PersistanceCIs::class.java)) {
    @Entity
    @Table(name = "collated_Indents")
    class PersistanceCIs(
            @Column(name = "ciNo")
            var ciNo: String,

            @ElementCollection
            var borrowerName: Set<INDENTState>,

            @Column(name = "party")
            var party: String
    ) : PersistentState() {
        // Default constructor required by hibernate.
        constructor(): this("", setOf(), "")
    }
}

但是我收到了以下休眠异常。

E 23:39:14+0530 [main] internal.Node.run - Exception during node startup {}

 org.hibernate.MappingException: Could not determine type for: com.example.state.INDENTState, at table: CollatedIndentsSchemaV1$PersistanceCIs_borrowerName, for columns: [org.hibernate.mapping.Column(borrowerName)]

    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:455) ~[hibernate-core-5.2.6.Final.jar:5.2.6.Final]

    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:422) ~[hibernate-core-5.2.6.Final.jar:5.2.6.Final]

    at org.hibernate.mapping.Collection.validate(Collection.java:310) ~[hibernate-core-5.2.6.Final.jar:5.2.6.Final]

    at org.hibernate.mapping.Set.validate(Set.java:27) ~[hibernate-core-5.2.6.Final.jar:5.2.6.Final]

    at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:333) ~[hibernate-core-5.2.6.Final.jar:5.2.6.Final]

    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444) ~[hibernate-core-5.2.6.Final.jar:5.2.6.Final]

    at net.corda.nodeapi.internal.persistence.HibernateConfiguration.buildSessionFactory(HibernateConfiguration.kt:113) ~[corda-node-api-corda-3.0.jar:?]

    at net.corda.nodeapi.internal.persistence.HibernateConfiguration.makeSessionFactoryForSchemas(HibernateConfiguration.kt:63) ~[corda-node-api-corda-3.0.jar:?]

    at net.corda.nodeapi.internal.persistence.HibernateConfiguration.access$makeSessionFactoryForSchemas(HibernateConfiguration.kt:26) ~[corda-node-api-corda-3.0.jar:?]

    at net.corda.nodeapi.internal.persistence.HibernateConfiguration$sessionFactoryForSchemas$1.apply(HibernateConfiguration.kt:44) ~[corda-node-api-corda-3.0.jar:?]

    at net.corda.nodeapi.internal.persistence.HibernateConfiguration$sessionFactoryForSchemas

有什么见解可以解决上述情况吗?

1 个答案:

答案 0 :(得分:0)

您不能将INDENTState直接存储为状态实例。您需要定义一个实体来表示数据库中的INDENTState

以下是another question中的一个示例:

object TestSchema : MappedSchema(SchemaFamily::class.java, 1, setOf(Parent::class.java, Child::class.java)) {
    @Entity
    @Table(name = "Parents")
    class Parent : PersistentState() {
        @OneToMany(fetch = FetchType.LAZY)
        @JoinColumns(JoinColumn(name = "transaction_id", referencedColumnName = "transaction_id"), JoinColumn(name = "output_index", referencedColumnName = "output_index"))
        @OrderColumn
        @Cascade(CascadeType.PERSIST)
        var children: MutableSet<Child> = mutableSetOf()
    }

    @Entity
    @Table(name = "Children")
    class Child {
        @Id
        @GeneratedValue
        @Column(name = "child_id", unique = true, nullable = false)
        var childId: Int? = null

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumns(JoinColumn(name = "transaction_id", referencedColumnName = "transaction_id"), JoinColumn(name = "output_index", referencedColumnName = "output_index"))
        var parent: Parent? = null
    }
}