Tuesday 30 July 2013

Hibernate: Using Transformers.aliasToBean(Class)

"Creates a resulttransformer that will inject aliased values into instances of Class via property methods or fields."

Usage examples:


This is a nice technic to automatically set values from a ResultSet to the CustomerDTO attributes.

The bean properties must match the columns name specified in the SQL statement, otherwise org.hibernate.PropertyNotFoundException: Could not find setter for... is thrown.
To make sure that this does not happen we can use SQLQuery.addScalar() so that the column names matches the attributes name of our CustomerDTO attributes.

Also, by using addScalar() we save the overhead of using ResultSetMetaData to map the column names to class attributes.

References:
- Hibernate - NativeSQL

No comments:

Post a Comment