休眠标准投影
正如问题标题所说,我正在尝试制定一个仅查询几个表属性的投影标准。
所以我有一个 Person 表/类,它有大约 40 个属性。我希望我的标准能够获得动态数量的属性,比方说 10、11 或 12(SQL 术语 从人中选择名字、姓氏
),我是这样做的:
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;
它返回了 < code>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 多个属性,它占用了查询时间,我不喜欢它。我也在寻找一种替代解决方案,它将帮助我解决这个问题。我读过有关 ResultTransformer
的内容,但还没有找到如何在我的案例中使用它。
Well as the question title says, I am trying to make a projection criteria querying only couple of the table attributes.
So I have a Person Table/class and it has about 40 attributes. I want my criteria to get dynamical number of attributes, lets say 10, 11 or 12 (SQL terms select firstname, lastname from person
) and I was doing it like this:
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;
and it gives back that the retList
elements are not from the Person.class
:
INFO [AWT-EventQueue-0] (UserGroupManagerApp.java127) - [Ljava.lang.Object;@14b9b80
FATAL [AWT-EventQueue-0] (Login.java78) - java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to usergroupmanager.model.db.Person
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to usergroupmanager.model.db.Person
Please help, for now I am listing all the 40+ attr, and it takes up querying time and I do not like it. I am looking also an alternative solution which will help me solve this. I read about ResultTransformer
but havent found how to use it in my case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 criteria.setResultTransformer()
Hibernate 中提供了一些 Transformer。如果您的 Person 没有任何关联,请使用此:
但如果 Person 有任何关联,请考虑使用 http://github 上的 Seimos。 com/moesio/seimos
如果使用它代替 Criteria,可以节省大量代码。
You can use criteria.setResultTransformer()
There's some Transformers provided in Hibernate. If your Person has not any association use this:
But if Person has any association, consider use Seimos at http://github.com/moesio/seimos
A lot of code could be saved if you use it instead Criteria.
用简洁的语言表示
Object[]
无法转换为Person
。当您进行投影时,您将获得您选择的属性作为对象数组而不是水合实体。您的代码缺少
retlist
声明。我猜这是一个原始的List
,您可以将其转换为List
某处。只需将其替换为List
即可。Says in clean words
Object[]
cannot be cast toPerson
. When you do a projection, you will get the attributes you selected as an array of objects instead of a hydrated entity.Your code is missing the declaration of
retlist
. I guess it's a rawList
which you cast to aList<Person>
somewhere. Just replace that withList<Object[]>
.如果您在 Hibernate 中使用投影,则不会查询 Hibernate 创建对象所需的所有数据。因此 Hibernate 无法创建对象。
因此,来自投影的查询仅返回从查询返回的 SQL 数组,即它作为列表返回,并且您可以作为该数组中的普通条目访问字段。
If you use a projection in Hibernate you are not querying all the data Hibernate needs to create the objects. Thus Hibernate cannot create the objects.
Thus the query from a projection just returns an array of the SQL returned from the query ie it returns a s List and you access the fields as plain entries in that array.