如何使用数据库中的项填充ChoiceBox

时间:2014-05-11 09:09:59

标签: java hibernate javafx h2

我试图通过Hibernate从H2数据库填充带有项目(国家/地区)的ChoiceBox,但是ChoiceBox只会填充一些没有意义的奇怪项目,而不是实际的国家/地区名称;像这样的东西:

project.Forms.AddNew.DB.ItemsPOJO@5aa434

如何从数据库中获取实际国家/地区名称而不是上述值?

这些课程如下:

POJO课程:

@Entity(name = "InitialDBItems")
public class InitialDBItemsPOJO implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int countriesListID;
    private String countriesList;

    public String getCountriesList() {
        return countriesList;
    }

    public void setCountriesList(String countriesList) {
        this.countriesList = countriesList;
    }

    public int getCountriesListID() {
        return countriesListID;
    }

    public void setCountriesListID(int countriesListID) {
        this.countriesListID = countriesListID;
    }
}

持久存储到数据库中的countries数组:

public class InitialDBItems {

    static InitialDBItemsPOJO initialDBItemsPOJO = new InitialDBItemsPOJO();

    public static void persistCountries() {
        String[] countriesList = {
            "Afghanistan",
            "Albania",
            "Algeria",
            // More countries



    };
    for (String c : countriesList) {
        initialDBItemsPOJO.setCountriesList(c);
        new ManageItems().addItems(initialDBItemsPOJO);
        System.out.println(c);
    }

}

我如何从数据库中获取国家/地区:

public static ObservableList<InitialDBItemsPOJO> retrieveCountriesList() {

    ObservableList<InitialDBItemsPOJO> data;
    List countriesListListItems;

    String countriesListListItemsQuery = "from InitialDBItems";
    data = FXCollections.observableArrayList();

    countriesListListItems = new ManageItems().listItems(countriesListListItemsQuery);

    for (Iterator iterator = countriesListListItems.iterator(); iterator.hasNext();) {
        InitialDBItemsPOJO countriesListListItemsIt = (InitialDBItemsPOJO) iterator.next();

        data.add(countriesListListItemsIt);
    }
    return data;
}

}

希望你能提供帮助。提前谢谢大家。

1 个答案:

答案 0 :(得分:0)

project.Forms.AddNew.DB.ItemsPOJO@5aa434是您实体toString();的默认InitialDBItemsPOJO的值,一个简单的解决方法是覆盖它

  @Entity(name = "InitialDBItems")

public class InitialDBItemsPOJO实现Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int countriesListID;
private String countriesList;

public String getCountriesList() {
    return countriesList;
}

public void setCountriesList(String countriesList) {
    this.countriesList = countriesList;
}

public int getCountriesListID() {
    return countriesListID;
}

public void setCountriesListID(int countriesListID) {
    this.countriesListID = countriesListID;
}
public String toString(){
    return countriesList;
}

}

public static ObservableList retrieveCountriesList() {

ObservableList<InitialDBItemsPOJO> data;
List countriesListListItems;

String countriesListListItemsQuery = "from InitialDBItems";
data = FXCollections.observableArrayList();

countriesListListItems = new ManageItems().listItems(countriesListListItemsQuery);

for (Iterator iterator = countriesListListItems.iterator(); iterator.hasNext();) {
    InitialDBItemsPOJO countriesListListItemsIt = (InitialDBItemsPOJO) iterator.next();

    data.add(countriesListListItemsIt.getCountriesList());
}
return data;

}