I wonder if you could help me on this. My motive is to load objects (along with their parent objects) with specific properties dynamically.
Initially I kept adding constructors and it was really a pain to maintain/remember them easily when the number of properties increased and in a case when they are dynamic, everything failed, I asked myself, why would I fetch every other property which is not of my interest, pure performance issue! I even had joins on multiple tables as
Code:
List<someTable> someTableList = session.createQuery("from someTable someTableAlias
left join fetch someTableAlias.someParentA
left join fetch someTableAlias.someParentB
... and so on
)
As someTable has a mapping of many-to-one on these two parents, this one query was good enough to get me any field to be displayed. But there is a serious performance issue.
Not giving criteria a try and as I really like HQL, I tried the value injection via property methods or fields (does not need constructors) as
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();
This is working like a charm just like the way you suggested for createSQLQuery transformer, but what when 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
Code:
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();
The query portion st.someParent.someParentField as someParentField
should tell you what I am trying to do here.
I wonder if I am missing something here. I have not yet tried this in createSQLQuery transformer but if it works there it sure should work in HQL and criteria as well isn't it.
If there is none, I would want to request one!
Regards
Krishna