Prediction of hibernation criteria

Well, as the title of the question says, I'm trying to make forecast criteria that request only a couple of table attributes.

So, I have a Person / class table, and it has about 40 attributes. I want my criteria to get a dynamic number of attributes, say 10, 11 or 12 (SQL terms select firstname, lastname from person), and I did 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 returns that the elements are retListnot related to Person.class:

INFO [AWT-EventQueue-0] (UserGroupManagerApp.java127) - [Ljava.lang.Object; @14b9b80
FATAL [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, havent , .

+3
3

[Ljava.lang.Object; usergroupmanager.model.db.Person

Object[] Person. , , , .

retlist. , List, List<Person>. List<Object[]>.

+4

.setResultTransformer()

Transformers Hibernate. - , :

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

- , http://github.com/moesio/seimos

, .

+5

If you use the projection in sleep mode, you do not request all the data required to create the Hibernate objects. Therefore, Hibernate cannot create objects.

Thus, the query from the projection returns the SQL array returned from the query, i.e. returns the list s and you get access to the fields as simple entries in this array.

0
source

Source: https://habr.com/ru/post/1764295/


All Articles