Help

People using the Criteria API have either transparently or knowingly used a ResultTransformer . A ResultTransformer is a nice and simple interface that allows you to transform any Criteria result element. E.g. you can make any Criteria result be returned as a java.util.Map or as a non-entity Bean.

Criteria Transformers

Imagine you have a StudentDTO class:

public class StudentDTO {
  private String studentName;
  private String courseDescription;
  
  public StudentDTO() { }
      
  ...
} 

Then you can make the Criteria return non-entity classes instead of scalars or entities by applying a ResultTransformer:

List resultWithAliasedBean = s.createCriteria(Enrolment.class)
  .createAlias("student", "st").createAlias("course", "co")
  .setProjection( Projections.projectionList()
                   .add( Projections.property("st.name"), "studentName" )
                   .add( Projections.property("co.description"), "courseDescription" )
          )
          .setResultTransformer( Transformers.aliasToBean(StudentDTO.class) )
          .list();

 StudentDTO dto = (StudentDTO)resultWithAliasedBean.get(0);  

This is how ResultTransformer have been available since we introduced projection to the Criteria API in Hibernate 3.

It is just one example of the built in transformers and users can provide their own transformers if they so please.

Jealous programming

Since I am more a HQL/SQL guy I have been jealous on Criteria for having this feature and I have seen many requests for adding it to all our query facilities.

Today I put an end to this jealousy and introduced ResultTransformer for HQL and SQL in Hibernate 3.2.

HQL Transformers

In HQL we already had a kind of result transformers via the (select new http://www.hibernate.org/hib_docs/v3/reference/en/html/queryhql.html#queryhql-select) syntax, but for returning non-entity beans it only provided value injection of these beans via its constructor. Thus if you used the same DTO in many different scenarios you could end up having many constructors on this DTO purely for allowing the select new functionality to work.

Now you can get the value injected via property methods or fields instead, removing the need for explicit constructors.

List resultWithAliasedBean = s.createQuery(
  "select e.student.name as studentName," +
  "       e.course.description as courseDescription" +
  "from   Enrolment as e")
  .setResultTransformer( Transformers.aliasToBean(StudentDTO.class))
  .list();

StudentDTO dto = (StudentDTO) resultWithAliasedBean.get(0);

SQL Transformers

With native sql returning non-entity beans or Map's is often more useful instead of basic Object[]. With result transformers that is now possible.

List resultWithAliasedBean = s.createSQLQuery(
  "SELECT st.name as studentName, co.description as courseDescription " +
  "FROM Enrolment e " +
  "INNER JOIN Student st on e.studentId=st.studentId " +
  "INNER JOIN Course co on e.courseCode=co.courseCode")
  .addScalar("studentName")
  .addScalar("courseDescription")
  .setResultTransformer( Transformers.aliasToBean(StudentDTO.class))
  .list();

StudentDTO dto =(StudentDTO) resultWithAliasedBean.get(0);

Tip: the addScalar() calls were required on HSQLDB to make it match a property name since it returns column names in all uppercase (e.g. STUDENTNAME). This could also be solved with a custom transformer that search the property names instead of using exact match - maybe we should provide a fuzzyAliasToBean() method ;)

Map vs. Object[]

Since you can also use a transformer that return a Map from alias to value/entity (e.g. Transformers.ALIASTOMAP), you are no longer required to mess with index based Object arrays when working with a result.

List iter = s.createQuery(
  "select e.student.name as studentName," +
  "       e.course.description as courseDescription" +
  "from   Enrolment as e")
  .setResultTransformer( Transformers.ALIAS_TO_MAP )
  .iterate();

String name = (Map)(iter.next()).get("studentName");

Again, this works equally well for Criteria, HQL and native SQL.

Reaching Nirvana of native sql

We still miss a few things, but with the addition of ResultTranformer support for SQL and the other additions lately to the native sql functionality in Hibernate we are close to reach the Nirvana of native sql support.

Combined with StatelessSession you actually now got a very flexible and full powered sql executor which transparently can map to and from objects with native sql without any ORM overhead.

...and when you get tired of managing the sql, objectstate, lifecycles, caching etc. of your objects manually and want to benefit from the power of an ORM then you got it all readily available to you ;)

8 comments:
 
18. Apr 2006, 14:47 CET | Link
Joel
I always thought that the result transformers were provided to Criteria to give it the functionality you could get from using HQL with the "new" syntax.

"select new com.CustomerDTO(ent.id, ent.name) from com.CustomerEntity ent"

I haven't gotten to look at the result transformers myself, they must be more powerful than the simple use case I have.
ReplyQuote
 
18. Apr 2006, 19:10 CET | Link
Max Rydahl Andersen | max(AT)hibernate.org
yes, you are mostly correct.

select new is equal to some of the transformations you can do, but we don't have a way to do "setter injection" via "select new" and it did not work for sql queries. It does now.
 
02. Nov 2006, 18:34 CET | Link
Cablo
Transformer injection is perfect, but not works with compound entities.
For example: entity has primary key class (properties p1, p2) with name "id" and I want to set these properties via alias "id.p1" and "id.p2".
Transformer fails, because it not find bean property with name "id.p1" in base entity.

Problem is, that Transformer doesn't use "deep property search". Is it mistake???

Thank, Charles

 
22. Oct 2008, 07:00 CET | Link
This is a important addition especially to solve performance issues while being able to write efficient dynamic HQL queries. But, how do we modify the HQL transformer in case of loading a specific parent or another mapped entity property.

session.createQuery(
"select st.stNumber as stNumber, st.stDate as stDate "
+ " from SomeTable st "
+ " where st.someTableId < 1000")
.setResultTransformer( Transformers.aliasToBean(database.SomeTable.class))
.list();

works fine but what if I want to load some of its parents properties only, as lets say, SomeTable has a parent called SomedParent and I want to access one of the fields of this parent only

session.createQuery(
"select st.stNumber as stNumber, st.stDate as stDate, st.someParent.someParentField as someParentField "
+ " from SomeTable st "
+ " where st.someTableId < 1000")
.setResultTransformer( Transformers.aliasToBean(database.SomeTable.class))
.list();

This fails, so any ideas?
 
20. Mar 2009, 17:44 CET | Link

some basics information about ht:

http://hibernatetutorial.com/Hibernate-tutorial-SQL-in-hibernate.html

 
29. May 2009, 01:59 CET | Link
Jared OduLio

Just a few question on the sample code.

How did you able to cast Map to a String variable name? Does iterate() really returns a List?

 
03. Dec 2009, 17:51 CET | Link
Steve Cohen

addScalar() throws UnsupportedOperationException (in AbstractList.add()) when applied to a SQL query created by Session.getNamedQuery(). There seems to be a different path toward creating the scalar list on a Named Query. I think this is a bug. There may be a workaround, but I didn't have time to work it out. I punted used createSQLQuery().

Has anyone else encountered this and if so, how is it solved?

 
26. Dec 2009, 11:49 CET | Link
Parthiv

Thanks. your article has helped us a lot.. Neatly Described.. good job..

Post Comment