使用join和with子句时,HQL查询失败

时间:2014-03-14 15:32:40

标签: grails hql gorm

晚安,

我正在使用Grails,我正在尝试进行HQL查询

我有一个对象机会,在其中有一个对象实体,在实体内部有一个标题集合。每个Title对象可以是main还是not(main是一个布尔字段,显示哪个标题是默认标题)。所以我正在做的查询是:

从Opportunity中选择机会作为机会加入opportunity.entity.titles as entityTitle with entityTitle.isMain为true

但是此查询失败并显示以下消息:

org.hibernate.hql.ast.InvalidWithClauseException:with子句表达式未引用与子句相关联的from子句元素。

我尝试添加实体和标题表但仍然失败。如果我删除with子句它可以正常工作,但我需要过滤标题。

感谢。

2 个答案:

答案 0 :(得分:0)

我发现了问题。首先,我必须将实体加入商机,然后将标题加入商机。所以查询是这样的:

select opportunity from Opportunity as opportunity 
join opportunity.entity activeEntity 
  with activeEntity.isActive is true 
join activeEntity.titles entityTitle 
  with entityTitle.isActive is true

答案 1 :(得分:0)

尝试此查询:

select distinct op from Opportunity as op 
inner join op.entity as ent 
inner join ent.titles as tit 
with tit.isMain is true
相关问题