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.
If
Clobis 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 anInstanceCreatorhow to create an instance ofClob, or you have to write a customTypeAdapterto perform the conversion. SinceClob(and its implementationSerialClob) are not your own classes you should choose theTypeAdapterapproach, 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-fromJsonconversion, for example:TemplateDatawhich creates a newTemplateAuditDatabased on itTemplateAuditDataextendsTemplateData