Hibernate:查询与SubQuery的连接

时间:2012-10-22 07:53:07

标签: hibernate

我需要将以下查询转换为hibernate -

select * from ep_batch eb
INNER JOIN ep_slide slide ON eb.batch_scanner_id  = slide.slide_batch_id
WHERE eb.batch_scanner_id = 1 AND 
eb.batch_status = 
(
IF((SELECT count(batch_id) as cnt from ep_batch WHERE batch_scanner_id = 1) =1,'Ready','In Progress')
) ;

我确实有“幻灯片”和“批处理”pojo代表ep_slide和ep_batch resp。加入注释为 -

@OneToMany(mappedBy = "batch")
    private Set<Slide> slides;
@ManyToOne
    @JoinColumn(name = "batchId")
    private Batch batch;

我不太确定如何添加带有条件的子查询。 我正在尝试下面的事情 -

DetachedCriteria subCriteria = DetachedCriteria.forClass(Slide.class);
        subCriteria.setProjection(Projections.count("batchId"));

1 个答案:

答案 0 :(得分:0)

我还没有执行,但您可以尝试以下HQL,适当地替换实体属性。

SELECT b FROM Batch b, Slide s 
WHERE b.batch_scanner_id = s.slide_batch_id AND 
b.batch_status = 
(CASE WHEN ((SELECT COUNT(bt.batch_id) FROM Batch bt WHERE bt.batch_scanner_id = 1) = 1) THEN 'Ready' 
ELSE 'In Progress' 
END);