使用日期之间的hibernate hql/sql 连接未正确过滤

Hibernate hql/sql join using between dates not filtering properly

我希望有人能帮助我理解为什么会发生以下情况。我正在使用 hibernate 和 HQL,但在 sqlserver 或 hsqldb 中使用从 hibernate 生成的 sql 的结果是相同的 - 我的日期没有过滤我的 JOINed 表。

问题的症结在于具有多个连接的 hql 查询,其中我要根据日期过滤器过滤其中一个连接。结果错误地包含了我的日期范围之外的数据。因此,例如,如果 startMonthAndYear 和 endMonthAndYear 分别是 01/2015 和 12/2015,我看到的结果(来自 saAllocationList)超出了该范围(例如 01/2010)。

HQL 有一个限制,即我不能在 JOIN 本身中执行子查询,我被告知这是可行的。我的问题是,似乎我写的每一种方式都应该做同样的事情..但我当然不理解 b/c 他们都没有像我想要的那样过滤日期。

我最初的 HQL 尝试在 WHERE 子句中进行过滤。

SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYear
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN WorkGroup workgroup1_ ON staffingas0_.WorkGroupID=workgroup1_.ID 

LEFT OUTER JOIN PositionRole positionro2_ ON staffingas0_.PositionRoleID=positionro2_.ID 

INNER JOIN Employee employeeex3_ ON staffingas0_.EmployeeID=employeeex3_.ID 

INNER JOIN SAAllocation saallocati4_ ON staffingas0_.ID=saallocati4_.SAID 

  WHERE workgroup1_.ProgramWorkGroupID=? AND (saallocati4_.DateUnit BETWEEN ? AND ?)

SELECT sa 

FROM StaffingAssignment sa

 INNER JOIN sa.sAAllocationList

 INNER JOIN sa.workGroup wg

 LEFT JOIN sa.positionRole pr

 INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id AND sa.id IN 

           (SELECT saa.staffingAllocation.id

           FROM SAAllocation saa

           WHERE saa.sAAllocationPK.dateUnit 

             BETWEEN :startMonthAndYear AND :endMonthAndYear)

SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=? AND (staffingas0_.ID IN (SELECT saallocati5_.SAID FROM SAAllocation saallocati5_ WHERE saallocati5_.DateUnit BETWEEN ? AND ?))

SELECT sa

FROM StaffingAssignment sa

  INNER JOIN sa.sAAllocationList sal WITH sal.sAAllocationPK.dateUnit BETWEEN 

    :startMonthAndYear AND :endMonthAndYear

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID AND (saallocati1_.DateUnit BETWEEN ? AND ?) 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=?
SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN FETCH sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYearquery.setHint("javax.persistence.cache.storeMode","BYPASS");

query.setHint("javax.persistence.cache.retrieveMode","BYPASS");

上面生成如下sql:

SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYear
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN WorkGroup workgroup1_ ON staffingas0_.WorkGroupID=workgroup1_.ID 

LEFT OUTER JOIN PositionRole positionro2_ ON staffingas0_.PositionRoleID=positionro2_.ID 

INNER JOIN Employee employeeex3_ ON staffingas0_.EmployeeID=employeeex3_.ID 

INNER JOIN SAAllocation saallocati4_ ON staffingas0_.ID=saallocati4_.SAID 

  WHERE workgroup1_.ProgramWorkGroupID=? AND (saallocati4_.DateUnit BETWEEN ? AND ?)

SELECT sa 

FROM StaffingAssignment sa

 INNER JOIN sa.sAAllocationList

 INNER JOIN sa.workGroup wg

 LEFT JOIN sa.positionRole pr

 INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id AND sa.id IN 

           (SELECT saa.staffingAllocation.id

           FROM SAAllocation saa

           WHERE saa.sAAllocationPK.dateUnit 

             BETWEEN :startMonthAndYear AND :endMonthAndYear)

SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=? AND (staffingas0_.ID IN (SELECT saallocati5_.SAID FROM SAAllocation saallocati5_ WHERE saallocati5_.DateUnit BETWEEN ? AND ?))

SELECT sa

FROM StaffingAssignment sa

  INNER JOIN sa.sAAllocationList sal WITH sal.sAAllocationPK.dateUnit BETWEEN 

    :startMonthAndYear AND :endMonthAndYear

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID AND (saallocati1_.DateUnit BETWEEN ? AND ?) 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=?
SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN FETCH sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYearquery.setHint("javax.persistence.cache.storeMode","BYPASS");

query.setHint("javax.persistence.cache.retrieveMode","BYPASS");

我已经用几种不同的方式重写了 hsql:

在 where 子句中使用子查询来尝试获取要加载的 id 列表:

SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYear
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN WorkGroup workgroup1_ ON staffingas0_.WorkGroupID=workgroup1_.ID 

LEFT OUTER JOIN PositionRole positionro2_ ON staffingas0_.PositionRoleID=positionro2_.ID 

INNER JOIN Employee employeeex3_ ON staffingas0_.EmployeeID=employeeex3_.ID 

INNER JOIN SAAllocation saallocati4_ ON staffingas0_.ID=saallocati4_.SAID 

  WHERE workgroup1_.ProgramWorkGroupID=? AND (saallocati4_.DateUnit BETWEEN ? AND ?)

SELECT sa 

FROM StaffingAssignment sa

 INNER JOIN sa.sAAllocationList

 INNER JOIN sa.workGroup wg

 LEFT JOIN sa.positionRole pr

 INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id AND sa.id IN 

           (SELECT saa.staffingAllocation.id

           FROM SAAllocation saa

           WHERE saa.sAAllocationPK.dateUnit 

             BETWEEN :startMonthAndYear AND :endMonthAndYear)

SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=? AND (staffingas0_.ID IN (SELECT saallocati5_.SAID FROM SAAllocation saallocati5_ WHERE saallocati5_.DateUnit BETWEEN ? AND ?))

SELECT sa

FROM StaffingAssignment sa

  INNER JOIN sa.sAAllocationList sal WITH sal.sAAllocationPK.dateUnit BETWEEN 

    :startMonthAndYear AND :endMonthAndYear

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID AND (saallocati1_.DateUnit BETWEEN ? AND ?) 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=?
SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN FETCH sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYearquery.setHint("javax.persistence.cache.storeMode","BYPASS");

query.setHint("javax.persistence.cache.retrieveMode","BYPASS");

生成以下sql:

SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYear
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN WorkGroup workgroup1_ ON staffingas0_.WorkGroupID=workgroup1_.ID 

LEFT OUTER JOIN PositionRole positionro2_ ON staffingas0_.PositionRoleID=positionro2_.ID 

INNER JOIN Employee employeeex3_ ON staffingas0_.EmployeeID=employeeex3_.ID 

INNER JOIN SAAllocation saallocati4_ ON staffingas0_.ID=saallocati4_.SAID 

  WHERE workgroup1_.ProgramWorkGroupID=? AND (saallocati4_.DateUnit BETWEEN ? AND ?)

SELECT sa 

FROM StaffingAssignment sa

 INNER JOIN sa.sAAllocationList

 INNER JOIN sa.workGroup wg

 LEFT JOIN sa.positionRole pr

 INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id AND sa.id IN 

           (SELECT saa.staffingAllocation.id

           FROM SAAllocation saa

           WHERE saa.sAAllocationPK.dateUnit 

             BETWEEN :startMonthAndYear AND :endMonthAndYear)

SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=? AND (staffingas0_.ID IN (SELECT saallocati5_.SAID FROM SAAllocation saallocati5_ WHERE saallocati5_.DateUnit BETWEEN ? AND ?))

SELECT sa

FROM StaffingAssignment sa

  INNER JOIN sa.sAAllocationList sal WITH sal.sAAllocationPK.dateUnit BETWEEN 

    :startMonthAndYear AND :endMonthAndYear

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID AND (saallocati1_.DateUnit BETWEEN ? AND ?) 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=?
SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN FETCH sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYearquery.setHint("javax.persistence.cache.storeMode","BYPASS");

query.setHint("javax.persistence.cache.retrieveMode","BYPASS");

在 HQL 中使用 WITH 关键字尝试在 JOIN 本身中进行过滤:

SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYear
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN WorkGroup workgroup1_ ON staffingas0_.WorkGroupID=workgroup1_.ID 

LEFT OUTER JOIN PositionRole positionro2_ ON staffingas0_.PositionRoleID=positionro2_.ID 

INNER JOIN Employee employeeex3_ ON staffingas0_.EmployeeID=employeeex3_.ID 

INNER JOIN SAAllocation saallocati4_ ON staffingas0_.ID=saallocati4_.SAID 

  WHERE workgroup1_.ProgramWorkGroupID=? AND (saallocati4_.DateUnit BETWEEN ? AND ?)

SELECT sa 

FROM StaffingAssignment sa

 INNER JOIN sa.sAAllocationList

 INNER JOIN sa.workGroup wg

 LEFT JOIN sa.positionRole pr

 INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id AND sa.id IN 

           (SELECT saa.staffingAllocation.id

           FROM SAAllocation saa

           WHERE saa.sAAllocationPK.dateUnit 

             BETWEEN :startMonthAndYear AND :endMonthAndYear)

SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=? AND (staffingas0_.ID IN (SELECT saallocati5_.SAID FROM SAAllocation saallocati5_ WHERE saallocati5_.DateUnit BETWEEN ? AND ?))

SELECT sa

FROM StaffingAssignment sa

  INNER JOIN sa.sAAllocationList sal WITH sal.sAAllocationPK.dateUnit BETWEEN 

    :startMonthAndYear AND :endMonthAndYear

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID AND (saallocati1_.DateUnit BETWEEN ? AND ?) 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=?
SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN FETCH sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYearquery.setHint("javax.persistence.cache.storeMode","BYPASS");

query.setHint("javax.persistence.cache.retrieveMode","BYPASS");

生成以下sql:

SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYear
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN WorkGroup workgroup1_ ON staffingas0_.WorkGroupID=workgroup1_.ID 

LEFT OUTER JOIN PositionRole positionro2_ ON staffingas0_.PositionRoleID=positionro2_.ID 

INNER JOIN Employee employeeex3_ ON staffingas0_.EmployeeID=employeeex3_.ID 

INNER JOIN SAAllocation saallocati4_ ON staffingas0_.ID=saallocati4_.SAID 

  WHERE workgroup1_.ProgramWorkGroupID=? AND (saallocati4_.DateUnit BETWEEN ? AND ?)

SELECT sa 

FROM StaffingAssignment sa

 INNER JOIN sa.sAAllocationList

 INNER JOIN sa.workGroup wg

 LEFT JOIN sa.positionRole pr

 INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id AND sa.id IN 

           (SELECT saa.staffingAllocation.id

           FROM SAAllocation saa

           WHERE saa.sAAllocationPK.dateUnit 

             BETWEEN :startMonthAndYear AND :endMonthAndYear)

SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=? AND (staffingas0_.ID IN (SELECT saallocati5_.SAID FROM SAAllocation saallocati5_ WHERE saallocati5_.DateUnit BETWEEN ? AND ?))

SELECT sa

FROM StaffingAssignment sa

  INNER JOIN sa.sAAllocationList sal WITH sal.sAAllocationPK.dateUnit BETWEEN 

    :startMonthAndYear AND :endMonthAndYear

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID AND (saallocati1_.DateUnit BETWEEN ? AND ?) 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=?
SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN FETCH sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYearquery.setHint("javax.persistence.cache.storeMode","BYPASS");

query.setHint("javax.persistence.cache.retrieveMode","BYPASS");

但一切都返回相同的结果(包括我试图过滤的范围之外的日期)。

感谢您阅读,如果我可以提供更多信息,这将有助于让我知道这几天一直在摆弄什么都没有显示,而且我知道这是 b/c 我对 SQL 的理解不够好。谢谢!

编辑:

我添加了一个 sqlfiddle。 http://sqlfiddle.com/#!2/8348d/2 不幸的是,sqlfiddle 做了正确的事情(即过滤掉 2010 年的数据)。谁能让它做错事,然后向我解释原因?


我想通了,尽管它让我质疑我对hibernate中 JOIN 的理解。这个博客很好地解释了解决方案:

http://java-persistence-performance.blogspot.com/2012/04/objects-vs-data-and-filtering-join.html

我需要使用 JOIN FETCH:

SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYear
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN WorkGroup workgroup1_ ON staffingas0_.WorkGroupID=workgroup1_.ID 

LEFT OUTER JOIN PositionRole positionro2_ ON staffingas0_.PositionRoleID=positionro2_.ID 

INNER JOIN Employee employeeex3_ ON staffingas0_.EmployeeID=employeeex3_.ID 

INNER JOIN SAAllocation saallocati4_ ON staffingas0_.ID=saallocati4_.SAID 

  WHERE workgroup1_.ProgramWorkGroupID=? AND (saallocati4_.DateUnit BETWEEN ? AND ?)

SELECT sa 

FROM StaffingAssignment sa

 INNER JOIN sa.sAAllocationList

 INNER JOIN sa.workGroup wg

 LEFT JOIN sa.positionRole pr

 INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id AND sa.id IN 

           (SELECT saa.staffingAllocation.id

           FROM SAAllocation saa

           WHERE saa.sAAllocationPK.dateUnit 

             BETWEEN :startMonthAndYear AND :endMonthAndYear)

SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=? AND (staffingas0_.ID IN (SELECT saallocati5_.SAID FROM SAAllocation saallocati5_ WHERE saallocati5_.DateUnit BETWEEN ? AND ?))

SELECT sa

FROM StaffingAssignment sa

  INNER JOIN sa.sAAllocationList sal WITH sal.sAAllocationPK.dateUnit BETWEEN 

    :startMonthAndYear AND :endMonthAndYear

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID AND (saallocati1_.DateUnit BETWEEN ? AND ?) 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=?
SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN FETCH sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYearquery.setHint("javax.persistence.cache.storeMode","BYPASS");

query.setHint("javax.persistence.cache.retrieveMode","BYPASS");

通过使用 JOIN FETCH,它强制 hibernate 执行限定符作为 fetch 的一部分。这本质上是一个 EAGER 加载集合 JOINed in WITH the qualifier apply。我的查询所做的(我认为)只是加载了合格的 JOINed 行的 id,但是当我访问集合时,它加载了整个集合(LAZILY),这让我很失望,直到我注意到发生了延迟加载。

但是:正如博客所警告的那样 - 如果使用二级缓存,它会使缓存处于风险 b/c 当您限定 JOIN 时,您现在将值放入缓存中的方式确实与数据库。为了解决这个问题,您可以通过设置进一步告诉 hibernate 不要将内容放入缓存中:

SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYear
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN WorkGroup workgroup1_ ON staffingas0_.WorkGroupID=workgroup1_.ID 

LEFT OUTER JOIN PositionRole positionro2_ ON staffingas0_.PositionRoleID=positionro2_.ID 

INNER JOIN Employee employeeex3_ ON staffingas0_.EmployeeID=employeeex3_.ID 

INNER JOIN SAAllocation saallocati4_ ON staffingas0_.ID=saallocati4_.SAID 

  WHERE workgroup1_.ProgramWorkGroupID=? AND (saallocati4_.DateUnit BETWEEN ? AND ?)

SELECT sa 

FROM StaffingAssignment sa

 INNER JOIN sa.sAAllocationList

 INNER JOIN sa.workGroup wg

 LEFT JOIN sa.positionRole pr

 INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id AND sa.id IN 

           (SELECT saa.staffingAllocation.id

           FROM SAAllocation saa

           WHERE saa.sAAllocationPK.dateUnit 

             BETWEEN :startMonthAndYear AND :endMonthAndYear)

SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=? AND (staffingas0_.ID IN (SELECT saallocati5_.SAID FROM SAAllocation saallocati5_ WHERE saallocati5_.DateUnit BETWEEN ? AND ?))

SELECT sa

FROM StaffingAssignment sa

  INNER JOIN sa.sAAllocationList sal WITH sal.sAAllocationPK.dateUnit BETWEEN 

    :startMonthAndYear AND :endMonthAndYear

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

WHERE wg.programWorkGroupID = :id
SELECT staffingas0_.ID AS ID1_6_, (trimmed BY me) FROM StaffingAllocation staffingas0_ 

INNER JOIN SAAllocation saallocati1_ ON staffingas0_.ID=saallocati1_.SAID AND (saallocati1_.DateUnit BETWEEN ? AND ?) 

INNER JOIN WorkGroup workgroup2_ ON staffingas0_.WorkGroupID=workgroup2_.ID 

LEFT OUTER JOIN PositionRole positionro3_ ON staffingas0_.PositionRoleID=positionro3_.ID 

INNER JOIN Employee employeeex4_ ON staffingas0_.EmployeeID=employeeex4_.ID 

WHERE workgroup2_.ProgramWorkGroupID=?
SELECT sa 

FROM StaffingAssignment sa 

  INNER JOIN sa.workGroup wg

  LEFT JOIN sa.positionRole pr

  INNER JOIN sa.employee em

  INNER JOIN FETCH sa.sAAllocationList sal

WHERE wg.programWorkGroupID = :id

  AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYearquery.setHint("javax.persistence.cache.storeMode","BYPASS");

query.setHint("javax.persistence.cache.retrieveMode","BYPASS");

我没有使用二级缓存,我认为我不能用 HQL 完成上述操作,所以我不担心这个细节。

这让我很好奇 JOIN 在做什么,因为我认为它避免了 N 1 问题,但也许它只是加载一些 id 以避免必须进行数据库工作来确定要加载的 id ,但对象本身尚未加载。那是另一个问题/研究的主题。感谢那些花时间回答的人!


在使用 C# 和 NHibernate 时遇到了同样的问题。

您在数据库中的时间列是什么数据类型(DATEDATETIMETIMESTAMP)?查询中的数据类型必须与数据库中的相同。否则 WHERE 子句将无法正确应用。


相关推荐

  • Spring部署设置openshift

    Springdeploymentsettingsopenshift我有一个问题让我抓狂了三天。我根据OpenShift帐户上的教程部署了spring-eap6-quickstart代码。我已配置调试选项,并且已将Eclipse工作区与OpehShift服务器同步-服务器上的一切工作正常,但在Eclipse中出现无法消除的错误。我有这个错误:cvc-complex-type.2.4.a:Invali…
    2025-04-161
  • 检查Java中正则表达式中模式的第n次出现

    CheckfornthoccurrenceofpatterninregularexpressioninJava本问题已经有最佳答案,请猛点这里访问。我想使用Java正则表达式检查输入字符串中特定模式的第n次出现。你能建议怎么做吗?这应该可以工作:MatchResultfindNthOccurance(intn,Patternp,CharSequencesrc){Matcherm=p.matcher…
    2025-04-161
  • 如何让 JTable 停留在已编辑的单元格上

    HowtohaveJTablestayingontheeditedcell如果有人编辑JTable的单元格内容并按Enter,则内容会被修改并且表格选择会移动到下一行。是否可以禁止JTable在单元格编辑后转到下一行?原因是我的程序使用ListSelectionListener在单元格选择上同步了其他一些小部件,并且我不想在编辑当前单元格后选择下一行。Enter的默认绑定是名为selectNext…
    2025-04-161
  • Weblogic 12c 部署

    Weblogic12cdeploy我正在尝试将我的应用程序从Tomcat迁移到Weblogic12.2.1.3.0。我能够毫无错误地部署应用程序,但我遇到了与持久性提供程序相关的运行时错误。这是堆栈跟踪:javax.validation.ValidationException:CalltoTraversableResolver.isReachable()threwanexceptionatorg.…
    2025-04-161
  • Resteasy Content-Type 默认值

    ResteasyContent-Typedefaults我正在使用Resteasy编写一个可以返回JSON和XML的应用程序,但可以选择默认为XML。这是我的方法:@GET@Path("/content")@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})publicStringcontentListRequestXm…
    2025-04-161
  • 代码不会停止运行,在 Java 中

    thecodedoesn'tstoprunning,inJava我正在用Java解决项目Euler中的问题10,即"Thesumoftheprimesbelow10is2+3+5+7=17.Findthesumofalltheprimesbelowtwomillion."我的代码是packageprojecteuler_1;importjava.math.BigInteger;importjava…
    2025-04-161
  • Out of memory java heap space

    Outofmemoryjavaheapspace我正在尝试将大量文件从服务器发送到多个客户端。当我尝试发送大小为700mb的文件时,它显示了"OutOfMemoryjavaheapspace"错误。我正在使用Netbeans7.1.2版本。我还在属性中尝试了VMoption。但仍然发生同样的错误。我认为阅读整个文件存在一些问题。下面的代码最多可用于300mb。请给我一些建议。提前致谢publicc…
    2025-04-161
  • Log4j 记录到共享日志文件

    Log4jLoggingtoaSharedLogFile有没有办法将log4j日志记录事件写入也被其他应用程序写入的日志文件。其他应用程序可以是非Java应用程序。有什么缺点?锁定问题?格式化?Log4j有一个SocketAppender,它将向服务发送事件,您可以自己实现或使用与Log4j捆绑的简单实现。它还支持syslogd和Windows事件日志,这对于尝试将日志输出与来自非Java应用程序…
    2025-04-161