JSON Persist Creating List Objects

Asked

Viewed 254 times

1

Good afternoon, everyone,

I’m having a doubt, I try to persist a JSON through the API:

{
 ...
        "Analise": [{
            "nProcAnalitic": "SR-000446/2015",
            ...
        }]
...
}

And the service answers me 200:

...
  "Analise": [
    {
      "id": null,
      "lotEntity": null,
      "nProcAnalitic": "SR-000446/2015"
...

However without creating the listing object, only the parent object.

Mapping:

@Jsonproperty("Analyze") @Onetomany(mappedBy = "lotEntity") private list analysis;

...

@Manytoone @Joincolumn(name = "id_lot") Private Lotentity lotEntity;

1 answer

0


I think you just need to specify the property Cascade=ALL in the oneToMany Annotation, so JPA will persist/change/delete the child objects in cascade.

  • Actually, Cascade.ALL was missing, but the parent reference object within the listing did not save the id, leaving null... ... "Scan": [ { "id": 3, "lotEntity": null,

  • You must set the parent entity in the daughter entity manually. I advise you to use the @postPersist annotation in a method in the parent class, so the JPA insert the parent entity, and before inserting the children, it will invoke this method and you can set the parent object (this) in the child object (analyze).

  • Oops, it worked: @Postpersist public void setObject() { this.analysis.foreach(a -> a.setLotEntity(this)); } Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.