在我的数据库中,有一个表:

CREATE TABLE calibration_factor_pm1
(
sensor_id Integer REFERENCES sensor(id),
factor_a double precision NOT NULL,
factor_b double precision NOT NULL,
time_from_which_factor_is_used timestamp NOT NULL
)


并且由于将有其他类似的表,因此我想创建一个函数,以便可以向我传递有关要从哪个表中获取哪个因子(a或b)的信息。

我试图这样做:

private Double getCalibrationFactor(String sensorId, TableField<Record, TableImpl> factor, TableImpl PM, TableField<Record, TableImpl> sensorIdTableField, TableField<Record, TableImpl> timeFromWhichFactorIsUsed) {
        Double calibrationFactorAPM1 = 1.0;
        try {
            calibrationFactorAPM1 = Double.valueOf(Database.create
                    .select(factor) //CalibrationFactorPm1.CALIBRATION_FACTOR_PM1.FACTOR_A
                    .from(PM) //CalibrationFactorPm1.CALIBRATION_FACTOR_PM1
                    .where(sensorIdTableField.equal(Integer.valueOf(sensorId)))
                    .orderBy(timeFromWhichFactorIsUsed.desc())
                    .fetchOne()
                    .getValue(0)
                    .toString());
        } catch (NullPointerException e) {
            System.out.println("DEBUG: Can't find calibration factor 'a' for this sensor");
            e.printStackTrace();
        }
    }


但是,我在Cannot resolve method 'equal(java.lang.Integer)'行上得到了.where(sensorIdTableField.equal(Integer.valueOf(sensorId)))

如何正确处理呢?

@编辑

这是equal()期望的:
java - jOOQ-如何将列和表传递给函数?-LMLPHP

最佳答案

我建议在方法签名中使用更多抽象类型,尤其是因为您似乎知道传递给方法的参数的类型。这样您就可以从jooq的类型安全功能中受益。

private Double getCalibrationFactor(String sensorId,
                                    Field<Double> factor,
                                    Table<?> PM,
                                    Field<Integer> sensorIdTableField,
                                    Field<Timestamp> timeFromWhichFactorIsUsed) {
  Record1<Double> calibrationFactorAPM1 = Database.create
              .select(factor) //CalibrationFactorPm1.CALIBRATION_FACTOR_PM1.FACTOR_A
              .from(PM) //CalibrationFactorPm1.CALIBRATION_FACTOR_PM1
              .where(sensorIdTableField.equal(Integer.valueOf(sensorId)))
              .orderBy(timeFromWhichFactorIsUsed.desc())
              .fetchOne();
  return calibrationFactorAPM1 == null ? calibrationFactorAPM1.value1() : 1d;
}


您可能还希望传递Integer sensorId而不是String sensorId

09-26 23:16