Y
Y
yuki2021-10-24 14:57:27
Java
yuki, 2021-10-24 14:57:27

CRUD spring+hibernate: json passing and server processing?

There are 2 models
Users

@Entity
public class Users {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = true)
    private int yearOld;

    @JsonIgnore
    @Column(nullable = false)
    private String password;

    @OneToMany(targetEntity = Pet.class, cascade = CascadeType.ALL, mappedBy = "owner")
    private List<Pet> pets;
    // сеттеры, геттеры, конструкторы
}

Table Pet
@Entity
@DynamicUpdate
public class Pet {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = true)
    private short yearOld;

    @ManyToOne
    @JoinColumn
    private Users owner;
    // геттеры, сеттеры, конструкторы
}

Question : how to correctly pass json for java so that you can crud for a one-to-many model (especially update and create are of interest)? It is also interesting how to process it competently on the Java side.

That is, I am interested in 2 positions:
1. Optimality, so that it is not necessary to transfer extra fields if they are not entered into the table or auto-generated
2. In JSON in the one-to-many field (for example, when creating a user, he will have animals) to be transmitted only an array of animal IDs, and not completely all the information about them (partially intersects with the first point - optimality)

If any of this cannot be observed, then you can directly say so

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-10-26
@rstJkee

Good afternoon.
First, to implement your task, you need to pay attention to the DTO (data to object) pattern.
Also, if you have a Spring application, you can connect mappers for the convenience of mapping (converting) dto -> entity (and vice versa). You can use libs like modelMapper, MapStruct.
If you do not want to use either, then implement the interfaceConverter<S,T>

Optimal so that it is not necessary to transfer extra fields if they are not entered into the table or are auto-generated

On the client side, using js, you can check the object's fields for null and, in case of this, simply do not pass this field. Well, or you can pass this field and check for null & empty already on the server side.
In JSON in the one-to-many field (for example, when creating a user, he will have animals) so that only an array of animal ids is transmitted, and not all information about them (partially intersects with the first point - optimality)

To transfer the id of an animal/s from the front, it is necessary that they be in the database. If the animals are added before the user, then it is enough to find the animals by the passed id and assign these animals to the user through the setPets()or methodaddPet()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question