CrudRepository没有在CommandLineRunner中保存数据

时间:2020-07-23 22:26:29

标签: java spring spring-boot jpa spring-data-jpa

我正在尝试在应用程序通过命令行运行器启动时添加一些默认数据,但来自crudrepository的.save函数未保存数据。我已经在另一个commandlinerunner中尝试过了,效果很好。我不确定这堂课是什么问题。任何帮助表示赞赏!

我运行的其他类具有以下控制台输出

Hibernate: insert into user (name, role, id) values (?, ?, ?)
Saved user:User [name=jyj123, id=1, role=Admin]

无法使用的此类显示了Hibernate:插入****和Hibernate:选择****

CommandLineRunner类

@Component
public class FlightsRepositoryCommandLineRunner implements CommandLineRunner {

    @Autowired
    private FlightsRepository flightsRep;
    
    @Override
    public void run(String... args) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = df.parse("2020-12-10");
        Date d2 = df.parse("2020-12-11");
        Flights flights = new Flights("SQ404", "SIN", "LAX", "2045", "2140", "15h 55min", "Singapore Airlines", "$1050", d1, d2, false);
        flightsRep.save(flights);
        Date d3 = df.parse("2021-01-01");
        Date d4 = df.parse("2021-01-02");
        Flights flights1 = new Flights("UA356", "LAX", "SIN", "0045", "0940", "21h 30min", "United Airlines", "$941", d3, d4, false);
        flightsRep.save(flights1);
        Date d5 = df.parse("2020-09-15");
        Date d6 = df.parse("2020-09-16-");
        Flights flights2 = new Flights("EM544", "SIN", "LAX", "2045", "2140", "20h 35min", "Emirates", "$591", d5, d6, false);
        flightsRep.save(flights2);
        Date d7 = df.parse("2020-12-25");
        Date d8 = df.parse("2020-12-26");
        Flights flights3 = new Flights("SQ501", "SIN", "LAX", "2045", "2140", "19h 45min", "Singapore Airlines", "$1203", d7, d8, false);
        flightsRep.save(flights3);
        Date d9 = df.parse("2020-10-10");
        Date d10 = df.parse("2020-10-11");
        Flights flights4 = new Flights("MA403", "LAX", "SIN", "1945", "2340", "14h 55min", "Malaysia Airlines", "$2102", d9, d10, false);
        flightsRep.save(flights4);
    }

}

实体类

@Entity
public class Flights {

    @Id
    String flightNumber;
    String origin;
    String destination;
    String takeOffTime;
    String landingTime;
    String flightDuration;
    String airline;
    String price;
    @Future
    @DateTimeFormat(pattern="yyyy-MM-dd")
    Date takeOffDate;
    @Future
    @DateTimeFormat(pattern="yyyy-MM-dd")
    Date landingDate;
    boolean flightReturn;

    public boolean isFlightReturn() {
        return flightReturn;
    }

    public void setFlightReturn(boolean flightReturn) {
        this.flightReturn = flightReturn;
    }

    public Date getLandingDate() {
        return landingDate;
    }

    public void setLandingDate(Date landingDate) {
        this.landingDate = landingDate;
    }

    public String getFlightNumber() {
        return flightNumber;
    }

    public void setFlightNumber(String flightNumber) {
        this.flightNumber = flightNumber;
    }

    public String getOrigin() {
        return origin;
    }

    public void setOrigin(String origin) {
        this.origin = origin;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }


    public String getTakeOffTime() {
        return takeOffTime;
    }


    public void setTakeOffTime(String takeOffTime) {
        this.takeOffTime = takeOffTime;
    }

    public String getLandingTime() {
        return landingTime;
    }

    public void setLandingTime(String landingTime) {
        this.landingTime = landingTime;
    }

    public String getFlightDuration() {
        return flightDuration;
    }

    public void setFlightDuration(String flightDuration) {
        this.flightDuration = flightDuration;
    }

    public Date getTakeOffDate() {
        return takeOffDate;
    }

    public void setTakeOffDate(Date takeOffDate) {
        this.takeOffDate = takeOffDate;
    }

    protected Flights() {

    }

    public String getAirline() {
        return airline;
    }

    public void setAirline(String airline) {
        this.airline = airline;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public Flights(String flightNumber, String origin, String destination, String takeOffTime, String landingTime,
            String flightDuration, String airline, String price, Date takeOffDate, Date landingDate,
            boolean flightReturn) {
        super();
        this.flightNumber = flightNumber;
        this.origin = origin;
        this.destination = destination;
        this.takeOffTime = takeOffTime;
        this.landingTime = landingTime;
        this.flightDuration = flightDuration;
        this.airline = airline;
        this.price = price;
        this.takeOffDate = takeOffDate;
        this.landingDate = landingDate;
        this.flightReturn = flightReturn;
    }

    @Override
    public String toString() {
        return "Flights [flightNumber=" + flightNumber + ", origin=" + origin + ", destination=" + destination
                + ", takeOffTime=" + takeOffTime + ", landingTime=" + landingTime + ", flightDuration=" + flightDuration
                + ", airline=" + airline + ", price=" + price + ", takeOffDate=" + takeOffDate + ", landingDate="
                + landingDate + ", flightReturn=" + flightReturn + "]";
    }

1 个答案:

答案 0 :(得分:0)

未保存在数据库中。谢谢您的帮助