3

I am trying to create a register page to add flight details, while trying to insert Date and Time in mysql database, but i am getting error rejected value [22-02-2020]; and rejected value [12:00]; , could some one guide me how to resolve this.

My Flight Model class

@Entity
@Table(name="FLIGHT")
public class Flight {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int ID;
    private String flightnumber;
    @Column(name="operatingairlines")
    private String operatingAirlines;
    @Column(name="departurecity")
    private String departureCity;
    @Column(name="arrivalcity")
    private String arivalCity;
    @Column(name="departuredate")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date dateOfDeparture;
    @Column(name="departuretime")
    private Timestamp estimatedTravelTime;

    public int getID() {
        return ID;
    }

    public void setID(int iD) {
        ID = iD;
    }

    public String getFlightnumber() {
        return flightnumber;
    }

    public void setFlightnumber(String flightnumber) {
        this.flightnumber = flightnumber;
    }

    public String getOperatingAirlines() {
        return operatingAirlines;
    }

    public void setOperatingAirlines(String operatingAirlines) {
        this.operatingAirlines = operatingAirlines;
    }

    public String getDepartureCity() {
        return departureCity;
    }

    public void setDepartureCity(String departureCity) {
        this.departureCity = departureCity;
    }

    public String getArivalCity() {
        return arivalCity;
    }

    public void setArivalCity(String arivalCity) {
        this.arivalCity = arivalCity;
    }

    public Date getDateOfDeparture() {
        return dateOfDeparture;
    }

    public void setDateOfDeparture(Date dateOfDeparture) {
        this.dateOfDeparture = dateOfDeparture;
    }

    public Timestamp getEstimatedTravelTime() {
        return estimatedTravelTime;
    }

    public void setEstimatedTravelTime(Timestamp estimatedTravelTime) {
        this.estimatedTravelTime = estimatedTravelTime;
    }

    @Override
    public String toString() {
        return "Flight [ID=" + ID + ", flightnumber=" + flightnumber + ", operatingAirlines=" + operatingAirlines
                + ", departureCity=" + departureCity + ", arivalCity=" + arivalCity + ", dateOfDeparture="
                + dateOfDeparture + ", estimatedTravelTime=" + estimatedTravelTime + "]";
    }

}

Login Controller

package com.tcdc.FlightReservation.controller;

@Controller
public class LoginController {

    @Autowired
    UserRepository userRepository;


    @GetMapping("/showform")
    public String showRegistration() {
        return "register";
    }

    @PostMapping("/registerUser")
    public String SaveRegister(@ModelAttribute("user") User user) {
        //user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        System.out.println(user);
        userRepository.save(user);
        return "login";
    }

    @GetMapping("/showlogin")
    public String showLogin() {
        //userRepository.
        return "login";
    }

    @PostMapping("/LoginProcess")
    public String LoginProcess(@RequestParam("email") String email, @RequestParam("password") String password) {

        User user = userRepository.findByEmail(email);
        System.out.println(user);
        if((user.getPassword().equals(password))&&(user.getRole().contentEquals("0"))) {
            return "findFlights";
        }
        else if((user.getPassword().equals(password))&&(user.getRole().contentEquals("1"))) {
            return "AddFlight";
        }
        else {
        return "login";
        }

    }

}

FlightController

@Controller
public class FlightController {

    @Autowired
    FlightServices flightServices;

    @Autowired
    FlightRepository flightRepository;

    @PostMapping("/flightResult")
    public String findFlights(@RequestParam("from") String from,@RequestParam("to") String to,@RequestParam("departureDate") @DateTimeFormat(pattern="dd-MM-yyyy") Date departureDate, Model model) {

        //flightRepository.findFlight(from,to,departureDate);
        System.out.println(from+" "+to+" "+departureDate);
        List<Flight> flist = flightServices.searchFlight(from,to,departureDate);
        model.addAttribute("flightdetails", flist);
        System.out.println(flist);
        return "FlightResults";
    }


    @PostMapping("/SaveFlight")
    public String SaveFlight(@ModelAttribute("flight") Flight flight) {

        flight.getDateOfDeparture();

                    //flightRepository.save(flight);


        return "FlightResults";
    }


}

Error

2020-02-19 20:02:35.580  WARN 3048 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'flight' on field 'dateOfDeparture': rejected value [22-02-2020]; codes [typeMismatch.flight.dateOfDeparture,typeMismatch.dateOfDeparture,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [flight.dateOfDeparture,dateOfDeparture]; arguments []; default message [dateOfDeparture]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateOfDeparture'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.Column @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '22-02-2020'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [22-02-2020]]
Field error in object 'flight' on field 'estimatedTravelTime': rejected value [12:00]; codes [typeMismatch.flight.estimatedTravelTime,typeMismatch.estimatedTravelTime,typeMismatch.java.sql.Timestamp,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [flight.estimatedTravelTime,estimatedTravelTime]; arguments []; default message [estimatedTravelTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Timestamp' for property 'estimatedTravelTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.Column java.sql.Timestamp] for value '12:00'; nested exception is java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]]]

Mysql Table details

ID                    int(11)   NO  PRI 
flightnumber        varchar(50) NO      
operatingairlines   varchar(50) NO      
departurecity       varchar(50) NO      
arrivalcity         varchar(50) NO      
departuredate        date       NO      
departuretime        timestamp  NO      CURRENT_TIMESTAMP

1 Answer 1

2

You can do something like this. Just use Date with following annotation.

@Column(name="departuretime")
@Temporal(TemporalType.TIMESTAMP)
private Date estimatedTravelTime;
Sign up to request clarification or add additional context in comments.

3 Comments

Getting error : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property: com.tcdc.FlightReservation.Models.Flight.estimatedTravelTime
@VyTcdc you have to use java.util.Date
Change timestamp to java.util.Date but agan type mismatch error as above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.