【发布时间】:2011-04-11 02:31:19
【问题描述】:
正如问题标题所说,我正在尝试制定一个仅查询几个表属性的投影标准。
所以我有一个人表/类,它有大约 40 个属性。我希望我的标准能够获得属性的动态数量,比如说 10、11 或 12(SQL 术语 select firstname, lastname from person),我是这样做的:
Transaction tx = session.beginTransaction();
Criteria crit = session.createCriteria(Person.class);
crit.setCacheable(true);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("id"));
Criterias c = null;
for (int i = 0; i < checked.size(); i++) {
Attribute attr = checked.elementAt(i);
switch (attr) {
case LASTNAME:
projList.add(Projections.property("lastName"));
c = enumMap.get(attr);
if (c.isChanged()) {
String tmp = (String) c.getAnswer();
tmp = tmp.replace('*', '%');
crit.add(Restrictions.like("lastName", tmp));
crit.addOrder(Order.asc("lastName"));
}
case ...THE REST .....
}
crit.setProjection(projList);
retList = crit.list();
tx.commit();
return retList;
它返回retList 元素不是来自Person.class:
信息 [AWT-EventQueue-0] (UserGroupManagerApp.java127) - [Ljava.lang.Object;@14b9b80
致命 [AWT-EventQueue-0] (Login.java78) - java.lang.ClassCastException: [Ljava.lang.Object;无法转换为 usergroupmanager.model.db.Person java.lang.ClassCastException:[Ljava.lang.Object;无法转换为 usergroupmanager.model.db.Person
请帮忙,现在我列出了所有 40+ attr,它占用了查询时间,我不喜欢它。我也在寻找可以帮助我解决这个问题的替代解决方案。我阅读了有关 ResultTransformer 的信息,但还没有找到在我的情况下如何使用它。
【问题讨论】:
-
retList的定义是什么
-
列表 retList = null;一开始,我现在用泛型测试它 List
retList = null 和同样的东西
标签: java hibernate criteria hibernate-criteria restrictions