ormlite不知道如何为field airline存储class flight.scanner.modelfx.airlinefx使用其他类或自定义持久器

v64noz0r  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(198)

我正在学习javafx并创建我的项目“flight scanner”,在实现ormlite数据库时遇到了一个小问题。我的代码如下:
控制器

public class FlightController {

private FlightModel flightModel;
private AirlineModel airlineModel;
private CityModel cityModel;

@FXML
private ComboBox<AirlineFx> airlineComboBoxAirline;

@FXML
private ComboBox<CityFx> cityComboBoxDeparture;

@FXML
private ComboBox<CityFx> cityComboBoxArrival;

@FXML
private TextField priceTextField;

@FXML
private DatePicker dateOfDeparture;

@FXML
private Button addFlightButton;

@FXML
public void initialize(){
    this.flightModel = new FlightModel();
    this.airlineModel = new AirlineModel();
    this.cityModel = new CityModel();
    try {
        this.flightModel.init();
        this.airlineModel.init();
        this.cityModel.init();
    } catch (ApplicationException e) {
        DialogsUtils.errorDialog(e.getMessage());
    }
    this.airlineComboBoxAirline.setItems(this.airlineModel.getAirlineList());
    this.cityComboBoxDeparture.setItems(this.cityModel.getCityList());
    this.cityComboBoxArrival.setItems(this.cityModel.getCityList());
    initBindings();
}

private void initBindings() {
    this.addFlightButton.disableProperty().bind(priceTextField.textProperty().isEmpty());
}

public void onActionComboBoxAirline() {
    this.airlineModel.setAirline(this.airlineComboBoxAirline.getSelectionModel().getSelectedItem());
}

public void onActionComboBoxDeparture() {
    this.cityModel.setCity(this.cityComboBoxDeparture.getSelectionModel().getSelectedItem());
}

public void onActionComboBoxArrival() {
    this.cityModel.setCity(this.cityComboBoxArrival.getSelectionModel().getSelectedItem());
}

public void addFlightOnAction() {
        try {
        this.flightModel.saveFlightInDataBase(this.airlineComboBoxAirline.getSelectionModel().getSelectedItem(), this.cityComboBoxDeparture.getSelectionModel().getSelectedItem(), this.cityComboBoxArrival.getSelectionModel().getSelectedItem(), priceTextField.getText(), dateOfDeparture.getValue());

    } catch (ApplicationException e) {
        DialogsUtils.errorDialog(e.getMessage());
    }
    priceTextField.clear();
}

}数据库-模型-飞行
@databasetable(tablename=“flight”)公共类flight实现basemodel{

public Flight() {
}

@DatabaseField(generatedId = true)
private int id;

@DatabaseField(columnName = "AIRLINE")
private AirlineFx airline;

@DatabaseField(columnName = "DEPARTURE", foreign = true, foreignAutoRefresh = true, foreignAutoCreate = true, canBeNull = false)
private CityFx departure;

@DatabaseField(columnName = "ARRIVAL", foreign = true, foreignAutoRefresh = true, foreignAutoCreate = true, canBeNull = false)
private CityFx arrival;

@DatabaseField(columnName = "PRICE", canBeNull = false)
private String price;

@DatabaseField(columnName = "DATE")
private LocalDate releaseDate;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public AirlineFx getAirline() {
    return airline;
}

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

public CityFx getDeparture() {
    return departure;
}

public void setDeparture(CityFx departure) {
    this.departure = departure;
}

public CityFx getArrival() {
    return arrival;
}

public void setArrival(CityFx arrival) {
    this.arrival = arrival;
}

public String getPrice() {
    return price;
}

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

public LocalDate getReleaseDate() {
    return releaseDate;
}

public void setReleaseDate(LocalDate releaseDate) {
    this.releaseDate = releaseDate;
}

modelfx-flightfx类
公共舱flightfx{

private IntegerProperty id = new SimpleIntegerProperty();
private StringProperty departure = new SimpleStringProperty();
private StringProperty arrival = new SimpleStringProperty();
private StringProperty price = new SimpleStringProperty();
private StringProperty airline = new SimpleStringProperty();
private StringProperty date = new SimpleStringProperty();

public int getId() {
    return id.get();
}

public IntegerProperty idProperty() {
    return id;
}

public void setId(int id) {
    this.id.set(id);
}

public String getDeparture() {
    return departure.get();
}

public StringProperty departureProperty() {
    return departure;
}

public void setDeparture(String departure) {
    this.departure.set(departure);
}

public String getArrival() {
    return arrival.get();
}

public StringProperty arrivalProperty() {
    return arrival;
}

public void setArrival(String arrival) {
    this.arrival.set(arrival);
}

public String getPrice() {
    return price.get();
}

public StringProperty priceProperty() {
    return price;
}

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

public String getAirline() {
    return airline.get();
}

public StringProperty airlineProperty() {
    return airline;
}

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

public String getDate() {
    return date.get();
}

public StringProperty dateProperty() {
    return date;
}

public void setDate(String date) {
    this.date.set(date);
}

}
flightmodel类
公共类飞行模型{

private ObservableList<AirlineFx> airlineList = FXCollections.observableArrayList();
private ObjectProperty<AirlineFx> airline = new SimpleObjectProperty<>();

public void init () throws ApplicationException {
    FlightDao flightDao = new FlightDao(DbManager.getConnectionSource());
    List<Airline> airlines = flightDao.queryForAll(Airline.class);
    initAirlineList(airlines);
    DbManager.closeConnectionSource();
}

private void initAirlineList(List<Airline> airlines) {
    this.airlineList.clear();
    airlines.forEach(c->{
        AirlineFx airlineFx = ConverterAirline.converterToAirlineFx(c);
        this.airlineList.add(airlineFx);
    });
}

public void saveFlightInDataBase(AirlineFx airline, CityFx cityDeparture, CityFx cityArrival, String price, LocalDate date) throws ApplicationException {
    FlightDao flightDao = new FlightDao(DbManager.getConnectionSource());
    Flight flight = new Flight();
    flight.setAirline(airline);
    flight.setDeparture(cityDeparture);
    flight.setArrival(cityArrival);
    flight.setPrice(price);
    flight.setReleaseDate(date);
    flightDao.creatOrUpdate(flight);
    DbManager.closeConnectionSource();
    init();
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题