Search code examples
javajpapersistence

JPA AccessType.Property failing to retrieve value from nested classes


I'm attempting to parse JSON into an @Entity in order to persist the data in a table. I confirmed the parsing works but I have run into an issue when attempting to persist the data. In order to retrieve the needed value in the nested classes (two deep) I am using the @Access(AccessType.PROPERTY) annotation on a "helper" method.

My best guess is the Entity object is being created prior to retrieving a value from the nested classes and when it attempts to retrieve the "nested" value those class instances are NULL. As a result it is unable to map the value to the persistence field.

Below is what I ascertain to be the important part of the 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)])

Below is the JSON I'm parsing:

{
  "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
    }
  ]
}

I'm having no issue persisting the NAME, ID, HIERARCHY_ID, UPD and CRT dates. I am also able to LOG the href using the toString() method. However, I cannot seem to persist this value (see href in links-->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;
}

Following are the "nested" classes. I quickly fooled around with @Embeddable and @Embedded annotations in an attempt to make this work - and without putting much thought into it as my brain is now mush. I initially had these classes as static inner classes and then moved them out of the Entity class.

I spent about four hours writing and rewriting and I'm now swallowing my pride. Any help is appreciated.

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


}

Solution

  • Your @Transient annotation may be causing problem here.

    @Transient
    private String href;
    

    @Transient used to indicate that perticular field should not be persisted in underlying persistance system i.e. database.

    you may follow this link as well