Hello all,
I want to retrieve a collection of entity class instances through creation of a Detached Criteria.
Let us suppose that I have an entity class called Person with the following properties:
- id
- name
- surname
- address
Now, I only want to retrieve a collection of Persons which contain the name and surname. For this I use multiple projections like so:
Code:
DetachedCriteria criteria = DetachedCriteria.forClass(Person.class);
//Define the projection list
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("name"));
projList.add(Projections.property("surname"));
//Set the projection list in our detached criteria
criteria.setProjection(projList);
//Retrieve the list of persons
List<Person> personList = getHibernateTemplate().findByCriteria(criteria)
But what I actually retrieve with this approach is a collection of Object collections which contain the values of the properties I set in my projection list.
Ok, fine. So I decide to set a result transformer in hopes of actually retrieving Person instances and not Object collection instances:
Code:
//Set the projection list in our detached criteria
criteria.setProjection(projList);
//Create and set result transformer
ResultTransformer resultTransformer = Transformers.aliasToBean(E2KAccounts.class);
criteria.setResultTransformer(resultTransformer);
//Retrieve the list of persons
List<Person> personList = getHibernateTemplate().findByCriteria(criteria)
With this, I do retrieve a collection of Person but all properties are null and while going through the debugger I get the following String when selecting elementData of the created Person list:
Quote:
Detail formatter error:
An exception occurred: java.lang.NullPointerException
And this String when selecting a Person object within the list:
Quote:
com.sun.jdi.InvocationException occurred invoking method.
So, my question is how do I properly retrieve the collection of Person I want instead of a collection of Object collections? I don't want to have to iterate through these objects and manually create a Person instance for each and proceed in mapping data from one object to another. I want to directly retrieve a list of Persons with projected property data properly set.
I have used result transformers before when using native SQL. In that case, I defined scalar maps for data mapping purposes. Should I be doing something similar here?
Thank you!