什么是使用异物的字段的值查询的最佳方式?
假设我有这三个类别。
描述的单位金额UnitResult类:
@DatabaseTable
公共类UnitResult {
公共静态最后弦乐ID_FIELD_NAME =ID;
公共静态最后弦乐UNIT_COLUMN_NAME =单位;
公共静态最后弦乐RESULT_COLUMN_NAME =结果;
@DatabaseField(generatedId = TRUE,COLUMNNAME = ID_FIELD_NAME)
公共整数ID;
@DatabaseField(外资= TRUE,canBeNull =假,COLUMNNAME = UNIT_COLUMN_NAME)
公共单位单元;
@DatabaseField(canBeNull = TRUE,COLUMNNAME = RESULT_COLUMN_NAME)
公共整数结果= NULL;
}
这说明某些单位在市场股类(例如jiuce,点心等):
@DatabaseTable
公共类股{
公共静态最后弦乐ID_FIELD_NAME =ID;
公共静态最后弦乐TYPE_FIELD_NAME =类型;
@DatabaseField(ID = TRUE,COLUMNNAME = ID_FIELD_NAME)
公众诠释ID;
@DatabaseField(canBeNull =假,COLUMNNAME = TYPE_FIELD_NAME)
公共UnitType类型;
}
和单位类型的枚举:
公开枚举UnitType {
果汁,
饮料,
小吃,
NPD;
}
那么,如何可以查询所有 UnitResult
,其中单位
的类型是 UnitType.JUICES
?
所以,我怎么可以查询所有UnitResult其中,单位类型UnitType.JUICES?
要做到这一点 ORMLite 的方法是使用`Where.in(...)与子查询:在
//设置我们的单元表的子查询第一
QueryBuilder的<单元,整数GT; uQb = unitDao.queryBuilder();
。uQb.where()EQ(Unit.TYPE_FIELD_NAME,UnitType.JUICES);
//在UnitResult表的外部查询
QueryBuilder的< UnitResult,整数GT; urQb = unitResultDao.queryBuilder();
//在利用子查询
urQb.where()中(UnitResult.UNIT_COLUMN_NAME,uQb)。
名单< UnitResult>结果= urQb.query();
What is the best way for querying by using the value of foreign object's field?
Suppose I have these three classes.
UnitResult class which describes amount of Units:
@DatabaseTable
public class UnitResult {
public static final String ID_FIELD_NAME = "id";
public static final String UNIT_COLUMN_NAME = "unit";
public static final String RESULT_COLUMN_NAME = "result";
@DatabaseField(generatedId = true, columnName = ID_FIELD_NAME)
public Integer id;
@DatabaseField(foreign = true, canBeNull = false, columnName = UNIT_COLUMN_NAME)
public Unit unit;
@DatabaseField(canBeNull = true, columnName = RESULT_COLUMN_NAME)
public Integer result = null;
}
Unit class which describes certain Units in a market (for example jiuce, snack etc.):
@DatabaseTable
public class Unit {
public static final String ID_FIELD_NAME = "id";
public static final String TYPE_FIELD_NAME = "type";
@DatabaseField(id = true, columnName = ID_FIELD_NAME)
public int id;
@DatabaseField(canBeNull = false, columnName = TYPE_FIELD_NAME)
public UnitType type;
}
And Enum of Unit type:
public enum UnitType {
JUICES,
DRINKS,
SNACKS,
NPD;
}
So how can I query all UnitResult
where Unit
type is UnitType.JUICES
?
So how can I query all UnitResult where Unit type is UnitType.JUICES?
The way to do this in ORMLite is to use the `Where.in(...) with a sub-query:
// setup our sub-query on the Unit table first
QueryBuilder<Unit,Integer> uQb = unitDao.queryBuilder();
uQb.where().eq(Unit.TYPE_FIELD_NAME, UnitType.JUICES);
// outer query on UnitResult table
QueryBuilder<UnitResult,Integer> urQb = unitResultDao.queryBuilder();
// in using the sub-query
urQb.where().in(UnitResult.UNIT_COLUMN_NAME, uQb);
List<UnitResult> results = urQb.query();