休眠条件 API 和 "in clause"

Hibernate criteria API with "in clause"

我有原生 SQL,我想移动到 Hibenate 的标准 API,

这里是:

SELECT *
FROM HV_LMA_VI t0
WHERE t0.HV_ID IN
              (SELECT t2.HV_ID
               FROM Z_LSZERG_HV t4,
                    Z_LSZ_ERG t3,
                    HERSTELLUNGSVARIANTE t2
               WHERE ((t3.LSZ_ID = 204)
                      AND ((t4.HV_ID = t2.HV_ID)
                      AND ((t3.ERG_ID = t4.ERG_ID)
                      AND (t3.LSZ_ID = t4.LSZ_ID)))));

所有需要的实体 (HV_LMA_VI, Z_LSZERG_HV, Z_LSZ_ERG,) 都存在。

根据我对您的查询的理解,它应该类似于:

DetachedCriteria detachedCriteria = DetachedCriteria.forClass(HERSTELLUNGSVARIANTE.class)
    .createAlias("propertyToT3", "t3")
    .createAlias("t3.propertyToT4", "t4")
    .add(Restrictions.eq("t3.LSZ_ID", 204))
    .setProjection(Projections.property("HV_ID"));
session.createCriteria(HV_LMA_VI.class)
    .add(Property.forName("HV_ID").in(detachedCriteria))
    .list();