假设我们有两个表: dept 和 emp
现在我需要使用hibernate条件来获取此查询:
select d. from dept d, emp e
where d.id=e.dept_id
and d.name='HR' and e.age>25;
我试过以下:
Session session = (Session) getEntityManager().getDelegate();
Criteria criteria = session.createCriteria(Dept.class, "d");
criteria.createAlias("emp", "e");
criteria.add(Restrictions.like("d.name", name));
criteria.add(Restrictions.ge("e.age", age));
List<Dept> results = criteria.list();
但这给了我这个查询输出:
select d.* ,e.* from
(而不是d.*
)
任何人都可以提供帮助吗?
答案 0 :(得分:0)
通过考虑具有//Init by object creation
struct POD2{
int a;
int b;
char c;
};
POD2 stackPod2{};
POD2* heapPod2 = new POD2{};
属性的Dept
,您可以使用id,name
从Dept
表中选择列,
setProjection()
<强>更新强>
或者您可以将HQL用作:
Session session = (Session) getEntityManager().getDelegate();
Criteria criteria = session.createCriteria(Dept.class, "d");
criteria.createAlias("emp", "e");
criteria.setProjection( Projections.projectionList()
.add( Projections.property("d.id"), "id")
.add( Projections.property("d.name"), "name"));
criteria.add(Restrictions.like("d.name", name));
criteria.add(Restrictions.ge("e.age", age));
criteria.setResultTransformer(new AliasToBeanResultTransformer(Dept.class))
List<Dept> results = criteria.list();
所以你的代码将是:
select d from Dept d inner join d.emp