0

我正在尝试对以下类执行连接查询:

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 应该使用哪个表(类)来连接?

谢谢。

4

1 回答 1

0

问题是创建的查询与设备而不是特征连接:

很抱歉,我无法重现此内容。我已将以下测试添加到主干。如果您能弄清楚为什么我的测试有效但您的代码失败了,那将非常有帮助。

https://github.com/j256/ormlite-core/commit/78223c682aa0d7063c095afc0e26b466aeb618af

不幸的是,没有太多答案。

于 2013-07-22T17:37:46.753 回答