Answer the question
In order to leave comments, you need to log in
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;
// сеттеры, геттеры, конструкторы
}
@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;
// геттеры, сеттеры, конструкторы
}
Answer the question
In order to leave comments, you need to log in
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
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)
setPets()
or methodaddPet()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question