Hibernate Criteria等同于HQL

时间:2012-12-17 04:52:50

标签: java hibernate hql criteria

是否存在与以下HQL等效的hibernate条件查询?

“从TestTable中选择新的TestTable(t.id,t.param1,t.param2,t.param3)”

2 个答案:

答案 0 :(得分:1)

如果您要加载表格的某些列,则可能需要Projection

Criteria crit = getSession().createCriteria(TestTable.class, "t");
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("id"));
projectionList.add(Projections.property("param1"));
projectionList.add(Projections.property("param2"));
projectionList.add(Projections.property("param1"));
crit.setProjection(projectionList);
List results = crit.list();

答案 1 :(得分:0)

如果你要做的只是查询TestTable的行并获取TestTable hibernate对象,只需在你想要检索的Hibernate对象的类上创建一个Criteria对象。

Criteria crit = sess.createCriteria(TestTable.class);
List results = crit.list();