jucajl
jucajl

Reputation: 1195

ORM Lite Android Unknown column name in table name

I'm using ORM Lite in my android application, I have a problem to do a search by filtering for a foreign field, I'll post my classes below:

public class Estabelecimento {

    @DatabaseField(generatedId = true)
    private int id;

    @DatabaseField
    public static long codigo;

    @ForeignCollectionField(eager=false)
    private Collection<SituacaoProduto> situacoesProduto;

        ...
}

public class SituacaoProduto {

    @DatabaseField(generatedId = true)
    private int id;

    @DatabaseField
    private long codigo;

    @DatabaseField
    private String situacao;

    @DatabaseField(foreign=true)
    private Produto produto;

    @DatabaseField(foreign=true,foreignAutoRefresh=true)
    private Estabelecimento estabelecimento;

        ...
}

and this would be my find method:

public List<SituacaoProduto> findByEstabelecimento(Estabelecimento estabelecimento, Context context){

        List<SituacaoProduto> list = new ArrayList<SituacaoProduto>();

        try{
            Dao<SituacaoProduto, Integer> dao = getDatabase(context).getDao(SituacaoProduto.class);
            QueryBuilder<SituacaoProduto, Integer> qBuilder = dao.queryBuilder();
            // Monta select: (estabelecimento.codigo == codigoEstabelecimento)
            qBuilder.where().eq("estabelecimento.codigo", estabelecimento.getCodigo());
            PreparedQuery<SituacaoProduto> pQuery = qBuilder.prepare();

            // Recupera registro            
            list = dao.query(pQuery);

        }catch(SQLException e){
            e.printStackTrace();
            return null;
        }finally{
            if (database != null) {
                OpenHelperManager.releaseHelper();
                database = null;
            }
        }

        return list;
    }

I get the following error:

12-10 10:32:25.398: E/AndroidRuntime(19575): java.lang.IllegalArgumentException: Unknown column name 'estabelecimento.codigo' in table situacaoproduto

It's as if there was no reference to foreign cheve, if I put in my estabelecimento_id find method for searching it put me off errors, but need not filter by codigo, it is a code that comes from the Web Service:

Here I found a solution, (link) but I refuse to do this, declare class attributes as public static final is a bad practice

some help?

Upvotes: 1

Views: 1615

Answers (1)

Gray
Gray

Reputation: 116878

I think you want to a join here. You are trying to return the SituacaoProduto whose estabelecimento field's codigo is equal to some value, right?

So build a query using the estabelecimentoDao where the codigo field eq(...). Then join that query with the situacaoProdutoDao query.

For details about ORMLite's join capabilities, see here:

http://ormlite.com/docs/join-queries

The example query in the docs is very similar to your needs I think.

declare class attributes as public static final is a bad practice".

Why is it a bad pattern? How else would the ORM library know what field you were querying on? The problem is that if you don't specify the column-name, ORMLite generates one for your foreign field. The only way to them query on that field (if you don't want to use hte above join mechanism) is to specify the name and then export that so your query code can use it. Sharing the same string as a public static final just makes sense to me.

Upvotes: 1

Related Questions