英文:
CrudRepository not saving data in CommandLineRunner
问题
我正在尝试在应用程序启动时通过CommandLineRunner添加一些默认数据,但crudrepository的.save函数未保存数据。我已经尝试在另一个CommandLineRunner中执行此操作,它正常工作。我不确定这个类中的问题是什么。感谢任何帮助!
我的另一个运行类显示以下控制台输出
Hibernate: insert into user (name, role, id) values (?, ?, ?)
Saved user:User [name=jyj123, id=1, role=Admin]
这个不工作的类显示Hibernate: insert **** 和 Hibernate: Select****
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;
// 省略了getter和setter以及其他方法
protected Flights() {
}
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;
}
// toString方法
}
希望这有助于解决问题。
英文:
I'm trying to add some default data the moment the application starts through commandlinerunner but the .save function from crudrepository is not saving the data. I have tried this in another commandlinerunner and it works fine. I'm not sure what is the problem in this class. Any help is appreciated!
My other class that runs has the following console output
Hibernate: insert into user (name, role, id) values (?, ?, ?)
Saved user:User [name=jyj123, id=1, role=Admin]
This class that doesn't work shows Hibernate: insert **** and Hibernate: Select****
CommandLineRunner class
@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 class
@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 + "]";
}
专注分享java语言的经验与见解,让所有开发者获益!
评论