我正在尝试对以下类执行连接查询:
public class EquipmentCharacteristic {
@DatabaseField(canBeNull = false, foreign = true, uniqueCombo = true)
private Equipment equipment;
@DatabaseField(canBeNull = false, foreign = true, uniqueCombo = true)
private Characteristic characteristic;
...
}
public class Characteristic {
...
}
public class Equipment {
...
}
查询生成器:
QueryBuilder<EquipmentCharacteristic, ?> daoEC = entityDAO.getDAO(
EquipmentCharacteristic.class).queryBuilder();
daoEC.where().eq(EquipmentCharacteristic.EQUIPMENT + "_id", equipment.getId());
QueryBuilder<Characteristic, ?> daoCharacteristic = entityDAO.getDAO(
Characteristic.class).queryBuilder();
return daoEC.join(daoCharacteristic)
.selectRaw(
"EquipmentCharacteristic." + EquipmentCharacteristic.ID,
"EquipmentCharacteristic." + EquipmentCharacteristic.EQUIPMENT + "_" + Equipment.ID,
"EquipmentCharacteristic." + EquipmentCharacteristic.POSSIBLE_VALUES,
"EquipmentCharacteristic." + EquipmentCharacteristic.CHARACTERISTIC + "_" + Characteristic.ID,
"EquipmentCharacteristic." + EquipmentCharacteristic.VALUE,
"Characteristic." + Characteristic.FIXED_VALUE,
"Characteristic." + Characteristic.UNIT,
"Characteristic." + Characteristic.CHARACTERISTIC)
.queryRaw().closeableIterator();
问题是创建的查询与设备而不是特征连接:
SELECT
EquipmentCharacteristic.id,
EquipmentCharacteristic.equipment_id,
EquipmentCharacteristic.possibleValues,
EquipmentCharacteristic.characteristic_id,
EquipmentCharacteristic.value,
Characteristic.fixedValue,
Characteristic.unit,
Characteristic.characteristic
FROM `EquipmentCharacteristic` INNER JOIN `Characteristic` ON `EquipmentCharacteristic`.`equipment_id` = `Characteristic`.`id`
如果我在 EquipmentCharacteristic 中将 Characteristic 引用放在 Equipment 之前,则不会出现问题。
public class EquipmentCharacteristic {
@DatabaseField(canBeNull = false, foreign = true, uniqueCombo = true)
private Characteristic characteristic;
@DatabaseField(canBeNull = false, foreign = true, uniqueCombo = true)
private Equipment equipment;
...
}
有没有办法通知 QueryBuilder 应该使用哪个表(类)来连接?
谢谢。