Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORMLite - How to handle result of join

I have tables A, B, C, D, E and I'm doing inner join across them.
I need only partial data from results. For example, I need A.name column, D.phoneNumber column.

I'm using the method join().

How should I handle the result? Should I go through all my classes, to get all the data I need? If yes, what is a simple way to do this, because going through all of that seems a bit complicated.

Should I create a class just for result and somehow map the result to it?

like image 583
Peter Avatar asked Jan 12 '13 21:01

Peter


1 Answers

According to the ORMlite documentation you can only use join statement with the tables connected via foreign object field.

So the first approach as I see it is to use foreign object and retreive all the values you need. For example if you query you tables A and B with

QueryBuilder<A, Integer> aqb = aDao.queryBuilder();
QueryBuilder<B, Integer> bqb = bDao.queryBuilder();
List<A> results = aqb.join(bqb).query();

for (A a : results){
    String name = a.getName();
    String phone = a.getB().getPhone();
}

whereas A and B classes looks something like this:

....
public class A{
....
    @DatabaseField
    private String name;

    @DatabaseField(foreign = true, foreignAutoRefresh = true)
    private B b;
....        
    public B getB(){
        return b;
    }
....
    public String getName(){
        return name;
    }
}

....
public class B{
....
    @DatabaseField
    private String phone;

    public String getPhone(){
        return phone;
    }
}

I'm not sure about exactly this code will work, I just wanted to describe the idea.

As I know you can't query for the multiple tables at once.

See the related ORMLite documentation. Join statements have been supported since version 4.42 of the library.

like image 197
rus1f1kat0R Avatar answered Sep 18 '22 14:09

rus1f1kat0R