Java 在Hibernate的公式字段中计算子表值
我在Hibernate属性公式字段中遇到了一个问题-我无法合并两个表列 shift\u id列属于job\u card的父表,duration列属于job\u card\u idle\u time的子表。但它认为这两列都属于job_card的父表 我想要这个Java 在Hibernate的公式字段中计算子表值,java,hibernate,Java,Hibernate,我在Hibernate属性公式字段中遇到了一个问题-我无法合并两个表列 shift\u id列属于job\u card的父表,duration列属于job\u card\u idle\u time的子表。但它认为这两列都属于job_card的父表 我想要这个 select (count(this_.shift_id)*340)-sum(**ir1_.duration**) /(count(this_.shift_id)*340) as y0_, this_.JOB_CA
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
如何实现这一点?这种类型的查询最好映射为查询,而不是派生属性 只能包含基于当前表中列的SQL表达式。使用其他表中的列的唯一方法是将公式编写为子选择:
(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中分组。此外,根据您的数据库,可能不支持在sub select中进行外部联接。+1由于精度问题,我删除了我的答案,但事实证明这是无用的。当您引用不带别名的列时,在查询执行期间,它将以主实体表的别名作为前缀。在hibernate 4.3.11中仍然是这样吗?我有一个简单的公式,其中父实体\u id没有别名:@FormulaSELECT countchild.\u id FROM Child Child where Child.\u parent\u\u id=\u id最后一个\u id应该引用包含该实体的父实体,但它在生成的sql中从来没有别名。我建议您投票选择您认为有用的答案,使用答案旁边的向上箭头。最后,当一个答案对你来说是正确的,你可以点击这个小标志来接受它。
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
(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)