2013-10-15 62 views
0

當升級我的數據庫模式我碰到一個問題與ForeignCollectionField(ormlite 4.7)不返回行。通過全新安裝應用程序,可以按預期方式添加和顯示行。Android Ormlite升級問題與ForeignCollectionField

當應用程序更新爲新版本時,架構會更新(請參閱下文),但是當行添加到數據庫時,集合不會返回添加的行。 (我可以看到數據庫中存在行)

更新前存在父行。我需要做些什麼來解決它?外商收集

父類中定義

@DatabaseTable(tableName = "setting") 
public class SettingEntity { 

    @DatabaseField(generatedId = true) 
    private long id; 

    … 

    //New field added 
    @ForeignCollectionField 
    private ForeignCollection<DistributionEntity> distribution; 

    public SettingEntity() { 
     // Required for ORMLite 
    } 

    public ForeignCollection<DistributionEntity> getDistribution() { 
     return distribution; 
    } 

    public void setDistribution(ForeignCollection<DistributionEntity> distribution) { 
     this.distribution = distribution; 
    } 
} 

兒童類

@DatabaseTable(tableName = "distribution") 
public class DistributionEntity { 

    @DatabaseField(generatedId = true) 
    private long id; 
    … 
    //New field added 
    @DatabaseField(canBeNull = true, foreign = true, index = true, foreignAutoRefresh = true, columnDefinition = "integer references setting(id) on delete cascade") 
    private SettingEntity setting; 

    public void setSetting(SettingEntity setting) { 

     this.setting = setting; 
    } 
} 

onUpgrade代碼

RuntimeExceptionDao<DistributionEntity, Integer> distributionDao = helper.getDistributionDao(); 
distributionDao.executeRaw("ALTER TABLE distribution ADD setting_id"); 
distributionDao.executeRaw("CREATE INDEX distribution_setting_idx ON distribution (setting_id)"); 

的ForeignCollectionField呼叫分配的調試信息

enter image description here

的代碼,在收集

public ArrayList<Distribution> getDistribution() { 
    getEntity().getDistribution().size(); 
    final ArrayList<Distribution> items = new ArrayList<Distribution>(); 
    final ForeignCollection<DistributionEntity> collection = getEntity().getDistribution(); 
    for (final DistributionEntity item : collection) { 
     final Distribution dist = new Distribution(item, mContext); 
     items.add(dist); 
    } 
    return items; 
} 

NB getEntity()迭代返回SettingEntity的實例

感謝花時間

+0

對不起舊響應。因此,您要向發佈中添加setting_id字段,但我沒有看到您將setting_id的值設置爲現有數據的適當'setting.id'字段。那是在別的地方完成的嗎? – Gray

回答

0

更多的解決方法不是回答,而是必須解決此問題。通過編寫代碼來複制行爲。

public List<DistributionEntity> getDistribution() { 

    List<DistributionEntity> distributionEntities = new ArrayList<DistributionEntity>(); 

    try { 
     DatabaseHelper helper = DatabaseHelper.getInstance(); 
     RuntimeExceptionDao<DistributionEntity, Integer> dao = helper.getDistributionDao(); 

     QueryBuilder<DistributionEntity, Integer> queryBuilder = dao.queryBuilder(); 
     queryBuilder.where().eq(DistributionEntity.SETTING_FIELD_NAME, Long.toString(this.getId())); 
     PreparedQuery<DistributionEntity> preparedQuery = queryBuilder.prepare(); 
     distributionEntities = dao.query(preparedQuery); 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

    return distributionEntities; 
    //return distribution; 
} 

愛知道真正的答案是