0

I am trying to convert an object to another using gson. while converting the gson getting a runtime exception saying: "unable to invoke no-args constructor for interface java.sql.clob. Registering an instancecreator with gson for this type may fix the problem." I am not quite sure why this exception is occuring. below is the code.

Entity 1:

    @Entity
@Table(name="Template")
public class TemplateData implements serializable{
    @Column(name="template_id")
    public Integer templateId;
    
    @Lob
    @Column(name="template_Data")
    public Clob templateData;

    @Lob
    @Column(name="template_Total")
    public Clob templateTotal;
    
    public Integer getTemplateId(){
        return templateId;
    }
    
    public void setTemplateId(Integer templateId){
        this.templateId = templateId;
    }
    
    public Clob getTemplateData(){
        return templateData;
    }
    
    public void setTemplateId(Clob templateData){
        this.templateData = templateData;
    }
    
    public Clob getTemplateTotal(){
        return templateTotal;
    }
    
    public void setTemplateTotal(Clob templateTotal){
        this.templateTotal = templateTotal;
    }
}

Entity 2-

    @Entity
@Table(name="Template_Audit")
public class TemplateAuditData implements serializable{

    @Column(name="template_Audit_id")
    public Integer templateAudtId;

    @Column(name="template_id")
    public Integer templateId;
    
    @Lob
    @Column(name="template_Data")
    public Clob templateData;

    @Lob
    @Column(name="template_Total")
    public Clob templateTotal;
    
    public Integer getTemplateAuditId(){
        return templateAudtId;
    }
    
    public void setTemplateId(Integer templateAudtId){
        this.templateAudtId = templateAudtId;
    }
    
    public Integer getTemplateId(){
        return templateId;
    }
    
    public void setTemplateId(Integer templateId){
        this.templateId = templateId;
    }
    
    public Clob getTemplateData(){
        return templateData;
    }
    
    public void setTemplateId(Clob templateData){
        this.templateData = templateData;
    }
    
    public Clob getTemplateTotal(){
        return templateTotal;
    }
    
    public void setTemplateTotal(Clob templateTotal){
        this.templateTotal = templateTotal;
    }
}

creating an object for templatedata and this object has some data.

TemplateData td = new TemplateData();
 

//td has some data and I am able to save the TemplateData using hibernate //trying to convert templatedata into templateauditdata

TemplateAuditData tad = new Gson().fromJson(new Gson().toJson(td), new TypeToken<TemplateAuditData>(){}.getType());

object 'tad' is giving me the above runtime exception.

Can anyone help me with the fix please. I am unable to understand the issue. Thank you in advance.

kavya
  • 157
  • 2
  • 10

1 Answers1

0

If Clob is an interface, Gson cannot deserialize it by default because it does not know which classes implement that interface (if any) and for which of them it should create an instance. That is what the exception is basically saying, you have to specify through an InstanceCreator how to create an instance of Clob, or you have to write a custom TypeAdapter to perform the conversion. Since Clob (and its implementation SerialClob) are not your own classes you should choose the TypeAdapter approach, otherwise you (accidentally) depend on the implementation details of these classes because Gson uses reflection to access their fields. This would most likely not work in the latest JDK versions anymore.

However, maybe there is a cleaner solution to this toJson-fromJson conversion, for example:

  • add a function to TemplateData which creates a new TemplateAuditData based on it
  • or, if possible use inheritance so that TemplateAuditData extends TemplateData
Marcono1234
  • 5,856
  • 1
  • 25
  • 43