I am doing a book loan API with spring boot. I have 4 classes Person, book, ActionBook (Here it will be seen if it is a loan, or return), typeAction.
Person:
@Entity
@Table(name = "persona")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Persona implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Column(nullable = false, length = 8)
private int dni;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private boolean isActivo = true;
@JsonIgnore
@OneToMany(mappedBy = "persona", targetEntity = com.example.model.AccionLibro.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private List<AccionLibro> accionLibro;
}
Book:
@Entity @Table (name="book") @JsonIgnoreProperties ({"hibernateLazyInitializer", "handler"}) public class Book implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String title;
@Column(nullable = false)
private int cantidad;
@Column(nullable = false)
private boolean isActivo = true;
@JsonIgnore
@OneToMany(targetEntity = com.example.model.AccionLibro.class, mappedBy = "libro", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<AccionLibro> accionLibro;
public Libro() {
// TODO Auto-generated constructor stub
}}
ActionBook:
@Entity
@Table(name = "accionlibro")
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class AccionLibro implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@JsonIgnore
@ManyToOne(targetEntity = com.example.model.Persona.class, fetch = FetchType.LAZY)
private Persona persona;
@ManyToOne(targetEntity = com.example.model.Libro.class, fetch = FetchType.LAZY)
private Libro libro;
@JsonIgnore
@ManyToOne(targetEntity = com.example.model.TipoAccion.class, fetch = FetchType.LAZY)
private TipoAccion tipoAccion;
@Column(nullable = false)
private int cantidad;
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate fechaAccion;}
TipoAccion:
@Entity
@Table(name = "tipoAccion")
public class TipoAccion implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Enumerated(EnumType.STRING)
private TipoAccionE tipoAccion;
@OneToMany(targetEntity = com.example.model.AccionLibro.class, fetch = FetchType.LAZY, mappedBy = "tipoAccion", cascade = {CascadeType.REFRESH,CascadeType.MERGE}, orphanRemoval = true)
private List<AccionLibro> accionLibro;
public TipoAccion() {
// TODO Auto-generated constructor stub
}}
The problem goes when I want to add a new ActionBook. Action Driver Book.
@RestController public class AccionController { @Autowired private AccionService service;
@PostMapping(value = "/prestamo/persona/{idPersona}", produces = "application/json", consumes = "application/json")
public boolean setAccionLibro(@RequestBody AccionLibro accionLibro,@PathVariable Long idPersona) {
System.out.println(accionLibro);
return this.service.setPrestamo(accionLibro,idPersona);
}}
the Json I sent.
{
"libro": {
"id": 1,
"title": "NAda de NAda",
"cantidad": 5,
"activo": true
},
"cantidad": 1,
"fechaAccion": null,
"tipoAccion": {
"id": 1
}
}
As I was seeing is that you can not deserializar an array but the weird thing is that in my class ActionBook I do not have any array. How could I fix it?