Select 具有两个单向关系的HQL查询和连接
请您提供以下帮助: 我的课程如下:Select 具有两个单向关系的HQL查询和连接,select,join,hql,where-clause,Select,Join,Hql,Where Clause,请您提供以下帮助: 我的课程如下: 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 n
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
}
(由于各种原因,上述关系只能是单向的。)
我有一个方法,需要根据各种标准返回选定的MasterAssociation。我的一个新标准是只返回包含特定问题类型的调查的协会。现有的代码都是基于HQL的,所以我想保留它(我知道SQL中的解决方案,但不想转换所有现有代码,也不想仅为这种情况创建特殊的方法)
我该如何做以下工作?(这是伪代码)
我真的很感激任何帮助
谢谢。我想我已经弄明白了(下面是一些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) "
}
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) "
}