This is an 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?
The following code:
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?
For example, let's 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();
So any ideas?
Best Solution
You are almost right.
First, you create the
SomeTable
:And your query:
In this case, you don’t need to create a constructor for the
SomeTable
class.