我在Hibernate属性公式字段中遇到了一个问题--我无法将两个表列组合在一起。
shift_id列属于job_card的父表,duration列属于job_card_idle_time的子表。但是它认为这两列都属于job_card的父表。
<property
name="utilization"
formula="(count(shift_id)*340)-sum(duration)/(count(shift_id)*340)"
generated="never"
insert="false"
update="false"
type="float">
</property>使用生成的查询:
select (count(this_.shift_id)*340)-sum(**this_.duration**) /(count(this_.shift_id)*340) as y0_,
this_.JOB_CARD_DATE as y1_
from job_card this_
left outer join job_card_idle_time ir1_ on this_.JOB_CARD_ID=ir1_.JOB_CARD_ID
where this_.JOB_CARD_DATE between ? and ?
group by this_.JOB_CARD_DATE
order by this_.JOB_CARD_DATE desc我想要这样的。
select (count(this_.shift_id)*340)-sum(**ir1_.duration**)
/(count(this_.shift_id)*340) as y0_,
this_.JOB_CARD_DATE as y1_
from job_card this_
left outer join job_card_idle_time ir1_ on this_.JOB_CARD_ID=ir1_.JOB_CARD_ID
where this_.JOB_CARD_DATE between ? and ?
group by this_.JOB_CARD_DATE
order by this_.JOB_CARD_DATE desc我该如何做到这一点?
发布于 2009-10-12 16:00:01
这种类型的查询最好映射为查询而不是派生属性。
Formula只能包含基于current表中的列的SQL表达式。使用其他表中的列的唯一方法是将您的公式写为子select:
(SELECT count(card.shift_id) * 340 - sum(idleTime.duration) / (count(card.shift_id) * 340)
FROM job_card card
LEFT OUTER JOIN job_card_idle_time idleTime on card.job_card_id = idleTime.job_card_id
WHERE card.shift_id = shift_id)当您引用没有别名的列时,在查询执行期间,它将以您的主实体表的别名作为前缀。但是,请注意,上面的操作不会执行您想要的操作,因为您不能基于外部条件对子选择进行分组。此外,根据您的数据库,可能不支持sub-select中的左外部联接。
https://stackoverflow.com/questions/1553204
复制相似问题