Home > Blockchain >  JPA calling default constructor even during POST request
JPA calling default constructor even during POST request

Time:10-03

I didn't had a default constructor in my entity class in the beginning. Eventually found out that JPA requires a default constructor in entity class so I made one.

After adding the default constructor, even during post requests, JPA keeps calling default constructor which leads to incorrect initialisation of properties. For example, if you see the property called availableSeats, it is initialised to 100, but during post request only default constructor is called which leads to initialisation of availableSeats to 0.

This is extremely weird and I don't understand what am I doing wrong here.

@Entity
public class Flight {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @NotBlank(message = "Airline Name cannot be blank!")
    private String airlineName;

    @NotBlank(message = "From-Location cannot be blank!")
    private String fromLocation;

    @NotBlank(message = "To-Location cannot be blank!")
    private String toLocation;

    @NotBlank(message = "Airport Gate Number cannot be blank")
    private String gateNumber;

    // @NotBlank(message = "Boarding time cannot be blank")
    private ZonedDateTime dateTimeZone;

    private static final int INITIAL_SEAT_CAPACITY = 100;
    private int availableSeats;

    // constructor should not be able to set id
    public Flight(Long id, String airlineName, String fromLocation, String toLocation, String gateNumber, ZonedDateTime dateTimeZone, int availableSeats) {


        this.id = id;
        this.airlineName = airlineName;
        this.fromLocation = fromLocation;
        this.toLocation = toLocation;
        this.gateNumber = gateNumber;
        this.dateTimeZone = dateTimeZone;
        
        // setting up initial number of available seats
        this.availableSeats = INITIAL_SEAT_CAPACITY;
    }

    public Flight(){
    }

    // getters and setters        
}

Also adding FlightController.java code here

@RestController
@RequestMapping("/api/flights")
public class FlightController {   

    @Autowired
    FlightService flightService;

    @GetMapping(value = "/")
    public ResponseEntity<List<Flight>> getAllFlights(){
        return flightService.getAllFlights();
    }

    @PostMapping(value = "/")
    public ResponseEntity<String> createFlight(@Valid @RequestBody Flight flight){
        return flightService.createFlight(flight);
    }

    @GetMapping(value = "/{id}")
    public ResponseEntity<Flight> getFlightById(@PathVariable Long id){
        return flightService.getFlightById(id);
    }

    @DeleteMapping(value = "/{id}")
    public ResponseEntity<String> deleteFlight(@PathVariable Long id){
        return flightService.deleteFlight(id);
    }

}

CodePudding user response:

As @grigouille pointed out in the comments, JPA only uses default constructor. Hence, availableSeats should have been initialised in the default constructor too.

CodePudding user response:

Spring's controller uses default(zero argument) constructor for object creation and then uses it's setter methods for setting the values in the object. You cannot expect for spring to use parameterized constructor. So if you need to set some default values then do it in zero argument constructor.

  • Related