Json, Hibernate, Spring boot

1

I'm having 2 situations that should do the same thing but do not:

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 there is oneToMany relationship. If I run this code I will not have any problems:

@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 all objects of type B in the database. 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 make 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 that I submit in method 2

Following the logic of method 2, it should delete all items from the list of objects of class B and then make the save, but by analyzing the hibernate log .. it makes the selects and simply deduce there is nothing different. .. and does not do the deletes, but if I make some change in the data like changing the name of class A or the itemName of class B it does the updates

    
asked by anonymous 01.06.2018 / 21:08

0 answers