【问题标题】:HQL Query and Join with two unidirectional relationships具有两个单向关系的 HQL 查询和连接
【发布时间】:2013-12-09 14:36:38
【问题描述】:

请您帮忙解决以下问题:

我的课程如下:

class MasterAssociation {
    String name    
    Survey survey // can be null
    Object other
    // Lots of other attributes
}

class Survey {
    String name
    // Lots of other attributes
}

class SurveyQuestion {
    String name
    QuestionType type
    Survey survey
    // Lots of other attributes
}

(由于各种原因,上述关系只能是单向的。)

我有一个方法需要根据各种标准返回选定的 MasterAssociations。我拥有的一个新标准是仅返回具有包含特定问题类型的调查的 MasterAssociations。现有代码都是基于HQL的,所以我想保留它(我知道SQL中的解决方案,但并不想转换所有现有代码,也不想为这种情况创建特殊方法)。

如何执行以下操作? (这是伪代码)

select MasterAssociation ma

// lots of existing code using different flags to construct query
// based on name, etc.

if (newSpecialFlag) {
    where ma.survey is not null
    and where ma.survey has SurveyQuestion.type = QuestionType.XYZ
}

非常感谢任何帮助。

谢谢。

【问题讨论】:

    标签: select join hql where-clause


    【解决方案1】:

    我想我明白了(下面是一些 Groovy 语法,但它应该仍然可读)

    我在其他地方读到了有关修改原始 from 子句以包含辅助对象的信息,即使实际的 select 没有选择它。

    答案:

    String hql = "select ma from MasterAssociation as ma"
    
    if (newSpecialFlag) {
        hql += ", SurveyQuestion as sq "
        criteria += " AND sq.type = " + QuestionType.XYZ
        criteria += " AND ma.survey.id in (sq.survey.id) "
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-15
      • 2012-08-24
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 2012-08-19
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多