简体   繁体   English

Hibernate Criteria Projection

[英]Hibernate Criteria Projection

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. 所以我有一个Person Table /类,它有大约40个属性。 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: 我希望我的标准获得动态数量的属性,比如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;

and it gives back that the retList elements are not from the Person.class : 并且它返回retList元素不是来自Person.class

INFO [AWT-EventQueue-0] (UserGroupManagerApp.java127) - [Ljava.lang.Object;@14b9b80 INFO [AWT-EventQueue-0](UserGroupManagerApp.java127) - [Ljava.lang.Object; @ 14b9b80
FATAL [AWT-EventQueue-0] (Login.java78) - java.lang.ClassCastException: [Ljava.lang.Object; 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; 无法转换为usergroupmanager.model.db.Person java.lang.ClassCastException:[Ljava.lang.Object; cannot be cast to usergroupmanager.model.db.Person 无法强制转换为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. 请帮助,现在我列出所有40+ attr,它占用查询时间,我不喜欢它。 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. 我读到了关于ResultTransformer但还没有找到如何在我的情况下使用它。

You can use criteria.setResultTransformer() 你可以使用criteria.setResultTransformer()

There's some Transformers provided in Hibernate. Hibernate中提供了一些变形金刚。 If your Person has not any association use this: 如果您的人没有任何关联,请使用此:

criteria.setResultTransformer(Transformers.aliasToBean(Person.class));

But if Person has any association, consider use Seimos at http://github.com/moesio/seimos 但如果Person有任何关联,请考虑在http://github.com/moesio/seimos上使用Seimos

A lot of code could be saved if you use it instead Criteria. 如果您使用它而不是Criteria,可以保存很多代码。

[Ljava.lang.Object; [Ljava.lang.Object; cannot be cast to usergroupmanager.model.db.Person 无法强制转换为usergroupmanager.model.db.Person

Says in clean words Object[] cannot be cast to Person . 用干净的话说Object[]不能转换为Person 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 . 您的代码缺少retlist声明。 I guess it's a raw List which you cast to a List<Person> somewhere. 我猜这是一个原始List ,你在某处投射到List<Person> Just replace that with List<Object[]> . 只需用List<Object[]>替换它。

If you use a projection in Hibernate you are not querying all the data Hibernate needs to create the objects. 如果在Hibernate中使用投影,则不会查询Hibernate创建对象所需的所有数据。 Thus Hibernate cannot create the objects. 因此Hibernate无法创建对象。

Thus the query from a projection just returns an array of the SQL returned from the query ie it returns as List and you access the fields as plain entries in that array. 因此,来自投影的查询只返回从查询返回的SQL数组,即它返回List并且您将该字段作为该数组中的普通条目进行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM