一对多的例子,ORMLITE

时间:2012-08-06 09:38:51

标签: android one-to-many ormlite

我有一个问题:如何在两个表之间创建One2Many连接?

主类:区域

    @DatabaseTable(tableName = "AREA")
public class Area {

    public Area() { }

    @DatabaseField(generatedId = true)
    private int Id;

    public final static String AREA_ID = "AREA_ID";
    @DatabaseField(canBeNull = true, index = true, dataType = DataType.INTEGER, columnName = AREA_ID)
    public int AreaId;

    public final static String AREA_TITLE = "AREA_TITLE";
    @DatabaseField(canBeNull = true, index = true, dataType = DataType.STRING, columnName = AREA_TITLE)
    public String AreaTitle;

    public final static String AREA_LANG = "LANG";
    @DatabaseField(canBeNull = true, index = true, dataType = DataType.STRING, columnName = AREA_LANG)
    public String Language;

    @ForeignCollectionField(eager = false)
    public Collection<City> CityList;

    //data service load
    public Area(int id, String title, Collection<City> cities, String language) {
        AreaId = id;
        AreaTitle = title;
        SetCities(cities);
        Language = language;
    }

    private void SetCities(Collection<City> cities) {
        CityList = cities;
    }
}

儿童班:

@DatabaseTable(tableName = "CITY")
public class City {

    public City() { }

    @DatabaseField(generatedId = true)
    private int Id;

    public final static String CITY_ID = "CITY_ID";
    @DatabaseField(canBeNull = true, index = true, dataType = DataType.INTEGER, columnName = CITY_ID)
    public int CityId;

    public final static String CITY_TITLE = "CITY_TITLE";
    @DatabaseField(canBeNull = true, index = true, dataType = DataType.STRING, columnName = CITY_TITLE)
    public String CityTitle;

    public final static String CITY_LANG = "LANG";
    @DatabaseField(canBeNull = true, index = true, dataType = DataType.STRING, columnName = CITY_LANG)
    public String Language;

    public final static String AREA_ID = "AREA_ID";
    @DatabaseField(foreign = true, columnName = AREA_ID, canBeNull = false ,foreignAutoCreate = true,foreignAutoRefresh = true)
    private Area Area;


    public City(int id, String title, String language) {
        CityId = id;
        CityTitle = title;
        Language = language;
    }
}

我在创建Area时没有任何例外,但CityList为空,我该如何解决?

提前致谢....

1 个答案:

答案 0 :(得分:1)

我认为您的Area类中的城市列表需要有eager = true