gpt4 book ai didi

java - JPA AccessType.Property 无法从嵌套类中检索值

转载 作者:行者123 更新时间:2023-12-01 11:24:39 26 4
gpt4 key购买 nike

我正在尝试将 JSON 解析为 @Entity,以便将数据保留在表中。我确认解析有效,但在尝试保留数据时遇到了问题。为了检索嵌套类(两层)中所需的值,我在“helper”方法上使用 @Access(AccessType.PROPERTY) 注释。

我最好的猜测是,实体对象是在从嵌套类检索值之前创建的,并且当它尝试检索“嵌套”值时,这些类实例为 NULL。因此,它无法将值映射到持久性字段。

以下是我确定的异常(exception)的重要部分:

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;
}

以下是“嵌套”类。我很快就开始使用 @Embeddable 和 @Embedded 注释来尝试完成这项工作 - 并且没有花太多心思,因为我的大脑现在已经很困惑了。我最初将这些类作为静态内部类,然后将它们移出实体类。

我花了大约四个小时写作和重写,现在我放弃了我的骄傲。如有任何帮助,我们将不胜感激。

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;
}


}

最佳答案

您的 @Transient 注释可能会导致问题。

@Transient
private String href;

@Transient 用于指示特定字段不应该被持久化在底层持久化系统即数据库中。

您可以关注this link还有

关于java - JPA AccessType.Property 无法从嵌套类中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30929378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com