-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: ResultTransformer with HQL
PostPosted: Mon Mar 12, 2007 3:14 pm 
Newbie

Joined: Tue Oct 25, 2005 10:40 am
Posts: 3
Hibernate version: 3.2.2GA

Hi,

I want to apply ResultTransformer to HQL query (Query interface has a setter setResultTransformer). So I did this:


List results = s.createQuery("select s.name, s.number from Student s") .setResultTransformer(Transformers.aliasToBean(Student.class)).list();


The problem is that it still returns list of arrays. Even if set transformer in not suitable here it is just omitted (debugged it ).

What is the problem? Is it only possible to apply transformer in native SQL or Criteria API?

Thanks,
Maciek


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2008 12:10 am 
Regular
Regular

Joined: Tue Feb 19, 2008 6:05 pm
Posts: 82
It works for me. But I have my own problems! I posted this problem in other places with no solution!

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

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

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

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.