【问题标题】:Hibernate hql/sql join using between dates not filtering properly使用日期之间的休眠 hql/sql 连接未正确过滤
【发布时间】:2015-02-17 16:45:38
【问题描述】:

我希望有人可以帮助我理解为什么会发生以下情况。我正在使用 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

上面生成的sql如下:

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 ?)

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

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

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)

生成以下sql:

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 ?))

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

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

生成以下sql:

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=?

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

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

编辑:

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

【问题讨论】:

  • 您可能想尝试将这些日期参数显式转换为查询中的日期...
  • 如果您包括表定义和示例数据,这将更容易分析,也许还有一些结果。例如,我不知道您在dateUnit 中有什么值,那么我如何判断它们是否应该包含在内?据我所知,它可能是CHAR(7),其值类似于'01/2010'
  • 类定义也不错 ;-) 可以,只是为了测试从查询中删除左连接部分吗?

标签: sql hibernate hql


【解决方案1】:

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

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

【讨论】:

  • 在数据库中类型是日期时间。在我的休眠代码中,类型是 java.util.Date。这两种类型应该匹配,我想。我的单元测试试图过滤 2010 年的异常值 .. 而包含的数据来自 01/2015-12/2015。我这样做是为了确保没有任何奇怪的午夜翻转逻辑或任何存在。
【解决方案2】:

我想通了,尽管它让我质疑我对休眠中 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 FETCH sa.sAAllocationList sal
WHERE wg.programWorkGroupID = :id
    AND sa.sAAllocationPK.dateUnit BETWEEN :startMonthAndYear AND :endMonthAndYear

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 2012-09-06
    相关资源
    最近更新 更多