Json, Hibernate, Spring boot

Asked

Viewed 139 times

1

I’m having two situations that should do the same thing but don’t:

package com.example.demo.entity;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import java.util.ArrayList;
import java.util.List;

@JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "id",
        scope = A.class)
public class A {

    public Integer id;
    public String name;
    public List<B> bItens = new ArrayList<>();

    public A() {
    }

    public A(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<B> getbItens() {
        return bItens;
    }

    public void setbItens(List<B> bItens) {
        this.bItens = bItens;
    }

    @Override
    public String toString() {
        return "A{" + "id=" + id + ", name=" + name + ", bItens=" + bItens + '}';
    }



}


package com.example.demo.entity;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "id",
        scope = B.class)
public class B {

    public Integer id;
    public String itemName;
    public A aOwner;

    public B() {
    }

    public B(Integer id, String itemName, A aOwner) {
        this.id = id;
        this.itemName = itemName;
        this.aOwner = aOwner;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public A getaOwner() {
        return aOwner;
    }

    public void setaOwner(A aOwner) {
        this.aOwner = aOwner;
    }

    @Override
    public String toString() {
        return "B{" + "id=" + id + ", itemName=" + itemName + ", aOwner=" + aOwner + '}';
    }

}

As you can see we have 2 classes where you have the oneToMany relationship. If I run this code I won’t have any problem:

@Autowired
    private ARepository ar;

@RequestMapping(value = "/teste1", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE}, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity teste1() {
        A a = ar.getOne(1);
        a.getbItens().clear();
        ar.save(a);
        return ResponseEntity.ok().body("");
    }

In other words, it will delete in the database all objects of type B. But if I make this code:

@Autowired
        private ARepository ar;

    @RequestMapping(value = "/teste2", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE}, produces = MediaType.APPLICATION_JSON_VALUE)
        public ResponseEntity teste2(@RequestBody A a) {
            a.getbItens().clear();
            ar.save(a);
            return ResponseEntity.ok().body("");
        }

It simply ignores delete and does not do the necessary deletions in the database...

{
    "id": 1,
    "name": "dasdsadsa",
    "bItens": [
        {
            "id": 1,
            "itemName": "sdfdsf",
            "aOwner": 1
        },
        {
            "id": 2,
            "itemName": "sdfdsf",
            "aOwner": 1
        },
        {
            "id": 3,
            "itemName": "sdfdsf",
            "aOwner": 1
        },
        {
            "id": 4,
            "itemName": "sdfdsf",
            "aOwner": 1
        }
    ]
}

Follow the json q I send in method 2

Following the logic of method 2, it should delete all items from the list of objects of class B and then save, but analyzing the Hibernate log.. it makes the selects and simply deduces it has nothing different... and it does not make the pins, however if I make any changes in the data like changing the name of class A or the item of class B it makes the updates

  • The problem is happening because of some Json problem, but I can’t identify how to get around this problem... is As if Hibernate could not identify objects originated by json to do all operations in the bank

  • You have already printed @Requestbody content?

  • However, in what I know about the use of Hibernate, if I simply instantiated an object of type A passing id 1 without filling in any other data and saving, it should do an update in registration number 1 and if it had any object attached to it it should remove.... but if I do it from a json, it doesn’t work

  • I already made a printf yes, it was with all the right information, in vdd I have 1 more way that I call to generate the json.. it comes with all the data in the correct way, could even post here if it is necessary

  • I believe that, in its second method, the object is not being managed by Hibernate.

  • Precisely, I am faithfully believing that something happens for Hibernate not to be managing this object generated by json, but how to make it be managed?

  • You will possibly not escape from doing the search you do in method 1 by passing the id or some other field of this object that arrives via parameter and only then deleting what you need. See if this complies with what you need and let me know, so I can create a response.

  • so in vdd what happens is this, the user will enter the first method and he will generate a json file q will be sent to him, he makes the modification in json and sends it to the second method.... however it will fall into the same situation in which I am passing...

  • What I am currently doing is this process that will later be used by the client: a url passes the json, I make the appropriate modifications using a system to manage clear and send the json to the other url that will be the test method2, only that when arriving at this method as the json is no longer being managed by Hibernate he will not know what q needs to delete and will not do the deletion, in this example I do not show my business rules, but the first method will receive an ID to know which registration to send and the second method will not only delete from the list

Show 4 more comments
No answers

Browser other questions tagged

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