How to update an attribute of a class that is related to another? Java web with Maven

0

I have the following method:

@RequestMapping (path="/", method=RequestMethod.POST)
    public ResponseEntity<?> inserir (@RequestBody Reserva reserva){
        reservaRepository.save(reserva);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

Reservation is a class that has a room, a client and a manager (in the reservation case would be the table N of my bank, the result of a relationship 1 to N) The fourth class (and table) has the 'busy' attribute. As long as it has the 'S' character, it means that it is busy, with the 'N' meaning that it is not busy. I was trying to make the reservation with the method above, change the 'busy' attribute of the room class that was informed during the reservation, I tried doing the following, however it gave error when calling the method:

@RequestMapping (path="/", method=RequestMethod.POST)
public ResponseEntity<?> inserir (@RequestBody Reserva reserva){
    reservaRepository.save(reserva.getQuarto().setOcupado("S"));
    return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

Below are the Fourth, Reserve and ReservationController classes

@Entity
@Table (name="quarto")
public class Quarto implements Serializable {

    private static final long serialVersionUID = 1L;
    public Quarto() {   }
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name="tipo")
    private String tipo;

    @Column(name="ocupado")
    private String ocupado;


    @OneToMany(mappedBy="quarto", cascade=CascadeType.ALL)
    @JsonIgnoreProperties("quarto")
    private List<Reserva> reserva;


    public Integer getId() {
        return id;
    }


    public void setId(Integer id) {
        this.id = id;
    }


    public String getTipo() {
        return tipo;
    }


    public void setTipo(String tipo) {
        this.tipo = tipo;
    }


    public String getOcupado() {
        return ocupado;
    }


    public void setOcupado(String ocupado) {
        this.ocupado = ocupado;
    }


    public List<Reserva> getReserva() {
        return reserva;
    }


    public void setReserva(List<Reserva> reserva) {
        this.reserva = reserva;
    }



    }

Reserve class:

@Entity
@Table (name="reserva")
public class Reserva implements Serializable {

    private static final long serialVersionUID = 1L;

    public Reserva() {}

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name="entrada")
    private String entrada;

    @Column(name="saida")
    private String saida;

    @ManyToOne
    @JoinColumn(name="id_quarto")
    @JsonIgnoreProperties("reserva")
    private Quarto quarto;

    @ManyToOne
    @JoinColumn(name="id_cliente")
    @JsonIgnoreProperties("reserva")
    private Cliente cliente;

    @ManyToOne
    @JoinColumn(name="id_gerente")
    @JsonIgnoreProperties("reserva")
    private Gerente gerente;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getEntrada() {
        return entrada;
    }

    public void setEntrada(String entrada) {
        this.entrada = entrada;
    }

    public String getSaida() {
        return saida;
    }

    public void setSaida(String saida) {
        this.saida = saida;
    }

    public Quarto getQuarto() {
        return quarto;
    }

    public void setQuarto(Quarto quarto) {
        this.quarto = quarto;
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public Gerente getGerente() {
        return gerente;
    }

    public void setGerente(Gerente gerente) {
        this.gerente = gerente;
    }




}

ReservationController class:

@RestController
@RequestMapping (path="/reservas")
public class ReservaController {

    @Autowired
    private ReservaRepository reservaRepository;

    @RequestMapping (path="/", method={RequestMethod.GET})
    public ResponseEntity<List<Reserva>> obterTodos(){
        return new ResponseEntity<List<Reserva>>(reservaRepository.findAll(), HttpStatus.OK);
    }

    @RequestMapping (path="/{id}", method=RequestMethod.DELETE)
    public ResponseEntity<?> excluir (@PathVariable("id") Integer id){
        reservaRepository.delete(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @RequestMapping (path="/", method=RequestMethod.POST)
    public ResponseEntity<?> inserir (@RequestBody Reserva reserva){
        reservaRepository.save(reserva);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @RequestMapping (path="/", method=RequestMethod.PUT)
    public ResponseEntity<?> atualizar(@RequestBody Reserva reserva){
        reservaRepository.save(reserva);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


}
    
asked by anonymous 30.08.2018 / 02:25

0 answers