等同于休眠条件的休眠{alias}

时间:2019-03-12 06:56:25

标签: hibernate alias criteria

我有一个这样的表实体:

class Parent {
     private Child lc;
     private Child rc;
     private Double price;
}

class Child {
     private Integer type;
     private Integer passed;
}

现在,我将为以下查询找到创建等效条件:

select 
    case when rc.passed = 0 then 1 else -1 end,
    p.price
from Parent p 
join Child lc on lc.id = p.lc
join Child rc on rc.id = p.rc
where lc.type = 0;

我创建这样的条件:

DetachedCriteria criteria = DetachedCriteria.forClass(Parent.class);
// create aliases and do Restrictions
ProjectionList pl = Projections.projectionList();

Type[] noeVoSar = { IntegerType.INSTANCE };
String[] colAliasNoeVoSar = { "noeVoSar" };

String sql = "(CASE WHEN passed = 0 THEN 1 ELSE -1 END)";
pl.add(Projections.sqlGroupProjection(sql + " as noeVoSar", sql, colAliasNoeVoSar, noeVoSar));

pl.add(Projections.groupProperty("price"));
criteria.setProjection(pl);

我的问题是在传递的字段中。左右关系都已通过字段,数据库说column ambiguously defined。我知道休眠中有{alias}指向根表别名。但我不知道有没有类似的解决方案?

我知道在JPA中有其标准,但我应该使用休眠状态

0 个答案:

没有答案