【发布时间】:2013-08-05 14:15:02
【问题描述】:
我想使用标准 api 从数据库中获取选定查询的数据。
例如:
select firstname,empid from emp where empid=10 &&
empname='bhanu' || salary=25000;
我该怎么做?
【问题讨论】:
标签: hibernate hibernate-criteria
我想使用标准 api 从数据库中获取选定查询的数据。
例如:
select firstname,empid from emp where empid=10 &&
empname='bhanu' || salary=25000;
我该怎么做?
【问题讨论】:
标签: hibernate hibernate-criteria
使用Criteria这样的查询:
Criteria criteria = session.createCriteria(Emp.class)
.setProjection( Projections.projectionList()
.add( Projections.property("firstName") )
.add( Projections.property("empId") ) );
Criterion criterion= Restrictions.and(Restrictions.eq("empId", 10),
Restrictions.eq("empName", "bhanu"));
criteria.add(Restrictions.or(criterion, Restrictions.eq("salary", 25000)));
List result=criteria.list();
【讨论】:
与普通 HQL 相比,使用 Criteria API 时需要进行一些设置。
Criteria API 文档非常好,如果您有特定问题,我建议您查看并回复。
如果这更像是一个设计问题,我会问您选择 Criteria 而不是 HQL 的原因是什么。
有具体要求吗?
【讨论】: