我试图将JSON解析为@Entity,以便将数据保存在表中。我确认解析工作,但我在尝试持久化数据时遇到了问题。为了在嵌套类(两个深层)中检索所需的值,我在"帮助器"上使用@Access(AccessType.PROPERTY)注释。方法
我最好的猜测是在从嵌套类中检索值以及何时尝试检索"嵌套"之前创建Entity对象。值那些类实例是NULL。因此,它无法将值映射到持久性字段。
以下是我确定的例外情况的重要部分:
Exception Description: The method [getNestedHref] on the object [com.something.dotcom.bleep.parsers.HierarchyV2] triggered an exception.
Internal Exception: java.lang.reflect.InvocationTargetException
Target Invocation Exception: java.lang.NullPointerException
Mapping: org.eclipse.persistence.mappings.DirectToFieldMapping[nestedHref-->schema.tbl_Hierarchy.HREF]
Descriptor: RelationalDescriptor(com.something.dotcom.bleep.parsers.HierarchyV2 --> [DatabaseTable(schema.tbl_Hierarchy)])
以下是我解析的JSON:
{
"links": {
"self": {
"href": "http:\/\/data.something.com\/v2\/hierarchy\/blah\/id\/blahblah"
}
},
"name": "nameValue",
"id": "idValue",
"hierarchyId": "hierarchyValue",
"narrowerTerm": [
{
"href": "http:\/\/data.something.com\/v2\/hierarchy\/blah\/id\/somethingElse1",
"sequence": 0
},
{
"href": "http:\/\/data.something.com\/v2\/hierarchy\/blah\/id\/somethingElse2",
"sequence": 1
},
{
"href": "http:\/\/data.something.com\/v2\/hierarchy\/blah\/id\/somethingElse3",
"sequence": 2
}
]
}
我在持续NAME,ID,HIERARCHY_ID,UPD和CRT日期方面没有任何问题。我也可以使用toString()方法记录href。但是,我似乎无法保持这个值(请参阅链接中的href - > self - > href)。
@JsonIgnoreProperties({"href","parentId","recordCreateDate","recordUpdateDate"})
@Entity
//@Cache(isolation = CacheIsolationType.ISOLATED)
@Table(name = "HIERARCHY_V2", schema = "SCHEMA")
@Access(AccessType.FIELD)
public class HierarchyV2{
@Id
private String id;
private String name;
@Column(name = "HIERARCHY_ID")
private String hierarchyId;
@Column(name = "PARENT_ID")
private String parentId;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "REC_CRT_DT")
private Date recordCreateDate;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "REC_UPD_DT")
private Date recordUpdateDate;
@Transient
private HierarchyLinks links;
@Transient
private List<HierarchyTerm> broaderTerm;
@Transient
private List<HierarchyTerm> narrowerTerm;
//Typical getters, setters, overridden methods....
@Access(AccessType.PROPERTY)
@Column(name = "HREF")
protected String getNestedHref(){
return this.links.getSelf().getHref();
}
protected void setNestedHref(String href){
HierarchyLinks links = new HierarchyLinks();
this.links = links;
HierarchyV2Self hvs = new HierarchyV2Self();
this.links.setSelf(hvs);
hvs.setHref(href);
}
@Override
public String toString(){
return this.name + "\t" + this.id + "\t" + this.hierarchyId + "\t" + this.getNestedHref() + "\t" + this.parentId;
}
以下是&#34;嵌套&#34;类。我很快就用@Embeddable和@Embedded注释来愚弄,试图让这项工作成功 - 而且没有经过深思熟虑,因为我的大脑现在已经糊里糊涂了。我最初将这些类作为静态内部类,然后将它们移出Entity类。
我花了大约四个小时写作和重写,现在我吞下了自己的骄傲。任何帮助表示赞赏。
public class HierarchyLinks {
private HierarchyV2Self self;
public HierarchyV2Self getSelf() {
return self;
}
public void setSelf(HierarchyV2Self self) {
this.self = self;
}
}
public class HierarchyV2Self {
private String href;
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
}
答案 0 :(得分:1)
你的@Transient注释可能会导致问题。
@Transient
private String href;
@Transient用于表示不应在基础持久性系统即数据库中保留特定字段。
您也可以关注this link