2

我想不通这个。

此查询在 sqlServer 中运行而没有错误:

SELECT     COUNT(tjmm.MAINMISSION_PARTICIPANT_SID)
FROM         T_MOVEMENT_LEG ml INNER JOIN 
              TJ_MVLG_PARTMOV tjmp ON ml.MOVEMENTLEG_SID = tjmp.MOVEMENTLEG_SID INNER JOIN 
              T_MAIN_MISSION mm INNER JOIN 
              T_MISSION_PARTICIPANTS mp ON mm.DETCO_MISSIONPARTICIPANT_SID = mp.MISSION_PARTICIPANT_SID INNER JOIN 
              T_PARTICIPANT_MOVEMENT pm ON mp.MISSION_PARTICIPANT_SID = pm.MISSION_PARTICIPANT_SID INNER JOIN 
              TJ_MMIS_MISPART tjmm ON mm.MAINMISSION_SID = tjmm.MAINMISSION_SID AND 
              mp.MISSION_PARTICIPANT_SID = tjmm.MISSION_PARTICIPANT_SID ON 
              tjmp.PARTICIPANTMOVEMENT_SID = pm.PARTICIPANTMOVEMENT_SID 
WHERE         (mm.IN_USE = 1) AND (mp.IS_OPS = 1) AND (pm.IS_OPS = 1) AND 
              (ml.LEGEFFECTIVE_STARTDATE < { fn NOW() }) AND (ml.LEGEFFECTIVE_ENDDATE > { fn NOW() }) AND 
              (ml.IS_CANCELED = 0)

将其转换为 @Formula :

@Formula("select count(tjmm.MAINMISSION_PARTICIPANT_SID) "
        + "FROM T_MOVEMENT_LEG ml INNER JOIN "
        + "TJ_MVLG_PARTMOV tjmp ON ml.MOVEMENTLEG_SID = tjmp.MOVEMENTLEG_SID INNER JOIN "
        + "T_MAIN_MISSION mm INNER JOIN "
        + "T_MISSION_PARTICIPANTS mp ON mm.DETCO_MISSIONPARTICIPANT_SID = mp.MISSION_PARTICIPANT_SID INNER JOIN "
        + "T_PARTICIPANT_MOVEMENT pm ON mp.MISSION_PARTICIPANT_SID = pm.MISSION_PARTICIPANT_SID INNER JOIN "
        + "TJ_MMIS_MISPART tjmm ON mm.MAINMISSION_SID = tjmm.MAINMISSION_SID AND "
        + "mp.MISSION_PARTICIPANT_SID = tjmm.MISSION_PARTICIPANT_SID ON "
        + "tjmp.PARTICIPANTMOVEMENT_SID = pm.PARTICIPANTMOVEMENT_SID "
        + "WHERE (mm.IN_USE = 1) AND (mp.IS_OPS = 1) AND (pm.IS_OPS = 1) AND "
        + "tjmm.MainMission_SID = MainMission_SID AND"
        + "(ml.LEGEFFECTIVE_STARTDATE < NOW()) AND (ml.LEGEFFECTIVE_ENDDATE > NOW()) AND "
        + "(ml.IS_CANCELED = 0)")
private Integer opsCount;

更改NOW()并添加了指向 pojo Id 的链接:tjmm.MainMission_SID = MainMission_SID

任何人都知道我在公式中的错误在哪里?

4

1 回答 1

4

小故事:用大括号包裹@Formula 的内容

// instead of this
// @Formula("select count(tjmm.MAINMISSION_PARTICIPANT_SID) "
// use this
@Formula("( select count(tjmm.MAINMISSION_PARTICIPANT_SID) "
          ...
         ")")

更长的故事:@Formula 映射表示“列式”表达式。如此处所述:

它可以包含任何导致 select 的语句,如下所示:

SELECT
  column1,           
  column1 + column2, // expecting columns contain number
  column1 * column2,
  ...

但这行不通

SELECT
  column1,           
  ...
  SELECT count(*) FROM OtherTable, // this won't work

因为这样的语句必须用大括号括起来

SELECT
  column1,           
  ...
  (SELECT count(*) FROM OtherTable), // working while wrapped with ()

这就是@Formula上面的问题

于 2014-10-06T18:03:19.840 回答