Home > database >  Spring-boot One-to-One mapping with POST method
Spring-boot One-to-One mapping with POST method

Time:12-04

Hello I am trying to make a reservation sysytem and create a reservation using a post method and assign one reservation to one item. Do you have any tips or advices?


@OneToOne(mappedBy = "car")
private Reservation reservation;
@OneToOne(cascade = CascadeType.ALL)
@MapsId
@JoinColumn(name = "fk_car_id")
private Car car;

I think mapping is correct - I have difficulties with creating a post method

@PostMapping("/car/{carId}")
Reservation addReservationWithCar(@RequestBody Reservation reservation, @PathVariable Long carId){
    Car car = carRepository.findById(carId);
    reservation.assignCar(car);
    return reservationRepository.save(reservation);

    //return ResponseEntity.status(201).body(this.carTypeRepository.save(carType));
}

CodePudding user response:

To create a reservation and assign it to a specific car using a POST method, you can use the code you provided as a starting point.

In your POST method, you can use the @PathVariable annotation to get the carId from the URL path, and then use it to retrieve the corresponding Car object from the carRepository. You can then call the assignCar method on the Reservation object to set the car field of the reservation to the retrieved Car object. Finally, you can use the save method of the reservationRepository to save the Reservation object and return it as the response of the POST method.

Here's an example of how you could do this:

@PostMapping("/car/{carId}")
Reservation addReservationWithCar(@RequestBody Reservation reservation, @PathVariable Long carId) {
    // Retrieve the car with the given carId
    Car car = carRepository.findById(carId);

    // Set the car field of the reservation to the retrieved car
    reservation.assignCar(car);

    // Save the reservation and return it as the response of the POST method
    return reservationRepository.save(reservation);
}

This code uses the findById method of the carRepository to retrieve the Car object with the given carId, and then sets the car field of the Reservation object to this Car object using the assignCar method. Finally, it saves the Reservation object and returns it as the response of the POST method.

CodePudding user response:

It looks like you have mapped your Reservation and Car entities correctly using the @OneToOne annotation. Your addReservationWithCar() method also looks correct.

Here are a few tips to consider:

  1. Make sure to validate the input data from the request body. For example, you can use the @Valid annotation in combination with a BindingResult object to validate the Reservation object before saving it to the repository.
  2. If you want to return the saved Reservation object as part of the response, you can use the ResponseEntity class to create a response with a suitable HTTP status code (e.g. 201 - Created) and the body of the response as the saved Reservation object.
  3. In the addReservationWithCar() method, you can use the orElseThrow() method on the Optional<Car> object returned by carRepository.findById() to either get the Car object or throw an exception if it is not found. This can help prevent errors if the Car with the given carId does not exist.

Here is an example of how you could update your addReservationWithCar() method to incorporate these suggestions:

@PostMapping("/car/{carId}")
ResponseEntity<Reservation> addReservationWithCar(@Valid @RequestBody Reservation reservation, BindingResult bindingResult, @PathVariable Long carId){
    if (bindingResult.hasErrors()) {
        throw new ValidationException(bindingResult);
    }

    Car car = carRepository.findById(carId).orElseThrow(() -> new EntityNotFoundException("Car not found with id: "   carId));
    reservation.assignCar(car);
    Reservation savedReservation = reservationRepository.save(reservation);
    return ResponseEntity.status(201).body(savedReservation);
}

I hope this helps! Let me know if you have any other questions.

  • Related