0

Using regular HQL I was able to get Hibernate to return a List of an object unmapped to the database by doing something along the lines of

List<MyUnmappedObject> result = (List<MyUnmappedObject>) session
               .createQuery("select new MyUnmappedObject(obj1.field1, obj2.field1, obj2.field2) from MyFirstObject obj1 join obj1.obj2 obj2")
               .list();

Now I am trying to do the same thing, but using Criteria. I know how to get a List using Projections, but is there any built-in support for new object creation using these results? Obviously I know that I can just parse the results and make the new object manually, but I'm trying to consolidate everything. I am currently using the following, but don't know how to incorporate this into direct object creation:

Criteria c = session.createCriteria(MyFirstObject.class, "obj1");
c.createAlias("obj1.obj2", "obj2");
c.setProjection(Projections.distinct(Projections.projectionList()
    .add(Projections.property("obj1.field1")
    .add(Projections.property("obj2.field1")
    .add(Projections.property("obj2.field2")));
List<Object[]> result = c.list();

Any help would be appreciated.

radar
  • 595
  • 2
  • 11
  • why can't you just map it? – dimoniy Aug 05 '14 at 16:09
  • It's a pre-existing class that's part of a very large project I don't want to/shouldn't edit. It doesn't correspond to a table in the database. – radar Aug 05 '14 at 16:14
  • you can extract the list of your `MyFirstObject` and then manually transform it to whatever you need. No need to use hibernate for that IMO. You can use something like Dozer to do transformations for you. – dimoniy Aug 05 '14 at 16:17

0 Answers0