Home > Software design >  Can not POST JSON with embedded objects - It reads and stores everything except the list of objects
Can not POST JSON with embedded objects - It reads and stores everything except the list of objects

Time:11-05

So I have this problem, when I try to post a JSON to the database using Postman it uploads it perfectly but it does not upload the embedded objects (which are in a list), I am using Spring Boot with JPA and hibernate, I am trying to create an API to make a CRUD with json files that all have the same structure.

In my case, I have Compania which is the main class and it has two Lists of objects, one being default_group, and the other one being Office.

Here is my controller(the POST method):

@PostMapping()
    public void createCompania(@RequestBody Compania compania) {
        companiaService.addCompania(compania);
    }

Here is my save method in my repository:

public void addCompania(Compania compania) {
        companiaRepository.save(compania);
        companiaRepository.flush();
    }

Here is my main entity(Compania) that has two OneToMany variables:

@Entity(name = "Compania")
@Table(
        name = "compania"
)
public class Compania {
    @Id
    @SequenceGenerator(
            name = "compania_sequence",
            sequenceName = "compania_sequence",
            allocationSize = 1
    )
    @GeneratedValue (
            strategy = GenerationType.SEQUENCE,
            generator = "compania_sequence"
    )
    @Column(
            nullable = false
    )
    private Long id;
    private String name;
    private String dominio;
    private String altas;
    private String bajas;
    @OneToMany(
            cascade = CascadeType.ALL,
            fetch = FetchType.LAZY,
            orphanRemoval = true
    )
    private List<DefaultGroup> default_group;
    @OneToMany(
            cascade = CascadeType.ALL,
            fetch = FetchType.LAZY
    )
    private List<Office> office;

Here is one of the other entities (default_group):

@Entity
@Table
public class DefaultGroup {
    @Id
    @SequenceGenerator(
            name = "defaultGroup_sequence",
            sequenceName = "defaultGroup_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "defaultGroup_sequence"
    )
    @Column(
            nullable = false
    )
    private Long id;
    private String name;
    private String path;
}

Here is the POST JSON I send with Postman:

{
"name": "name",
"dominio": "domain",
"altas": "xxxx",
"bajas": "yyyy",
"default_group":[
    {
        "name":"Users",
        "path":"pathExample"
        
    }
]
}

Here is the GET METHOD with the results

{
        "id": 3,
        "name": "name",
        "dominio": "domain",
        "altas": "xxxx",
        "bajas": "yyyy",
        "default_group": [
            {}
        ],
        "office": []
    }

As you can see, I'm not getting the default_group data, like if it wasn't parsing it correctly. Do you know what could be the problem? Or where could it be?

Thanks!

CodePudding user response:

The problem was that I didn't add the getters and setters for the other entities (Default_group and office) so it couldn't set the different parameters into the objects.

  • Related