Corda: error=org.hibernate.InstantiationException:

2019-07-09 07:36发布

问题:

I met a issue, please to help. Corda Code based on M13. My Schema code this is mainly use define a schema refer your sample.

object LegalContractSchemaV1 : MappedSchema(
        schemaFamily = LegalContractSchema.javaClass,
        version = 1,
        mappedTypes = listOf(PersistentLegalContractState::class.java)) {

    @Entity
    @Table(name = "legal_contract_states")
       class PersistentLegalContractState(
            @Column(name = "contract_title`enter code here`")
            var contractTitle: String,

            @Column(name = "sender_name")
            var senderName: String,

            @Column(name = "recipient_name")
            var recipientName: String,

            @Column(name = "status")
            var status: String) : PersistentState(){
      //  constructor(stateRef: StateRef) :  this(stateRef.txhash.bytes.toHexString(), stateRef.index)
    }

Corda Node error Log. E 12:53:45 [rpc-server-handler-pool-0] vault.HibernateVaultQueryImpl._queryBy - org.hibernate.InstantiationException: No default constructor for entity: : com.legalcontract.schema.LegalContractSchemaV1$PersistentLegalContractState E 12:53:45 [rpc-server-handler-pool-0] messaging.ObservableContext.sendMessage - Failed to send message, kicking client. Message was RpcReply(id=RpcRequestId(toLong=4982229886899153484), result=ErrorOr(value=null, error=org.hibernate.InstantiationException: No default constructor for entity: : com.legalcontract.schema.LegalContractSchemaV1$PersistentLegalContractState)) com.esotericsoftware.kryo.KryoException: Class org.hibernate.InstantiationException is not annotated or on the whitelist, so cannot be used in serialization Serialization trace: error (net.corda.core.ErrorOr) at net.corda.core.serialization.CordaClassResolver.checkClass(CordaClassResolver.kt:65) ~[corda-core-0.13.0.jar:?] at net.corda.core.serialization.CordaClassResolver.getRegistration(CordaClassResolver.kt:35) ~[corda-core-0.13.0.jar:?] at com.esotericsoftware.kryo.Kryo.getRegistration(Kryo.java:488) ~[kryo-4.0.0.jar:?] at net.corda.nodeapi.RPCKryo.getRegistration(RPCStructures.kt:74) ~[corda-node-api-0.13.0.jar:?] at com.esotericsoftware.kryo.util.DefaultClassResolver.writeClass(DefaultClassResolver.java:97) ~[kryo-4.0.0.jar:?]

回答1:

kotlin-noarg plugin generates default construtors for classes that have been annotated with selected annotations.

Apply for Kotlin-jpa plugin for annotated @Entity to generated default construtor.

Add the scripts to gradle file

buildscript {
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
    }
}

apply plugin: "kotlin-jpa"


回答2:

I had the same error (org.hibernate.InstantiationException: No default constructor for entity), also after installing the kotlin-jpa plugin.

Solution was to make all my fields in my custom schema nullable, e.g.:

object LegalContractSchemaV1 : MappedSchema(
    schemaFamily = LegalContractSchema.javaClass,
    version = 1,
    mappedTypes = listOf(PersistentLegalContractState::class.java)) {

@Entity
@Table(name = "legal_contract_states")
   class PersistentLegalContractState(
        @Column(name = "contract_title`enter code here`")
        var contractTitle: String? = null,

        @Column(name = "sender_name")
        var senderName: String? = null,

        @Column(name = "recipient_name")
        var recipientName: String? = null,

        @Column(name = "status")
        var status: String? = null) : PersistentState()


回答3:

You need to add a default constructor to the body of PersistentLegalContractState. Something like:

constructor() : this("", "", "", "")


标签: kotlin corda