Pergunta

I'm using ORMLite in my Android application, and I'm trying to persist and query a nested class. Creation and updating models is error-free, though I don't know if it's persisting the models how I'd expect. When I query for the model, I get the outer class's data fine, but it never gets the inner class data. Here's my model (stripped down for readability):

@DatabaseTable
public class Race {

    @DatabaseField(id = true)
    private String raceId;
    @DatabaseField
    private String name;
    @ForeignCollectionField
    private Collection<Rule> rules;

    @DatabaseTable
    public static class Rule {

        @DatabaseField
        private long ruleId;
        @DatabaseField
        private String name;
        @DatabaseField(foreign = true)
        private Race race;
    }
}

My add method:

public boolean addOrUpdateRace(Race race) {

    CreateOrUpdateStatus status = mHelper.getRaceDao().createOrUpdate(race);
    return status.isCreated() || status.isUpdated();
}

My query method:

public List<Race> getAllRaces() {

    return mHelper.getRaceDao().queryForAll();
}

The list from getAllRaces has all the expected variables populated (raceId and name) and has a ForeignCollection for rules. When I attempt to access rules, I get an iterator that has a size of 0.

I wasn't sure about what to do with the Race variaible in the Rule class, I just knew ORMLite threw errors when I didn't include it. I've tried leaving it null in the hopes that ORMLite would automagically make the connection with its parent, and when that didn't work I tried setting the race variable just before persisting, but that had the same effect.

Where have I gone wrong?

Foi útil?

Solução

The DB/Table for Rule doesn't appear to be being populated based on the code you provided.

You need to create a Dao for the 'Rule' class and have an appropriate addOrUpdateRule(Rule rule) used during parsing since ORMLite cannot automagically populate the DB/Table for Rule based on its association as a ForeignCollectionField within the 'Race' class.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top