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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With