-->
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: different behavior of Resulttransformer with HQL or Criteria
PostPosted: Fri Oct 10, 2008 9:57 am 
Newbie

Joined: Tue Dec 04, 2007 4:53 am
Posts: 5
I'm using hibernate 3.2.6ga and had the following classes and queries - I made the whole stuff a bit easier, so typos don't count ;-):

Code:
// mapped entities - all classes feature a (snipped) id
class Album{
Set<AlbumSong> albumSongs;
}

class AlbumSong{
Album album;
Song song;
}

class Song{
String name;
}

// this bean's not mapped
class SongCount {
long songCount;
Song songCountSong;
}


I use the following HQL get a list of SongCount objects (which Song is on how many albums)
Code:
Query query = session.createQuery("select count(alb_song.song.id) as songCount, alb_song.song as songCountSong from Album alb, IN(alb.albumSongs) alb_song group by alb_song.song.name");
query.setResultTransformer(Transformers.aliasToBean(SongCount.class));


the above code works likes charm, resulting in only one query giving me the right results. then i tried to do the same using the criteria API. like that:

Code:
final DetachedCriteria criteria = DetachedCriteria.forClass(Album.class, "alb");
criteria.createAlias("alb.albumSongs", "albSong");
criteria.createAlias("albSong.song", "s_song");

criteria.setProjection(Projections.projectionList()
   .add(Projections.count("albSong.song"), "songCount")
        .add(Projections   .property("albSong.song"), "songCountSong")
.add(Projections
                  .groupProperty("s_song.name")

            ));
      criteria.setFetchMode("albSong.song", FetchMode.JOIN);

      criteria.setResultTransformer(Transformers.aliasToBean(SongCount.class));


using the criteria API results in a similar query with one major difference: it's only retrieving the id of the Song class and performs an IN query afterwards instead of fetching all of the attributes with the first query.

Is there a way to avoid the additional query using the criteria API or do I have to stick with HQL?

thanks in advance!


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

Joined: Tue Feb 19, 2008 6:05 pm
Posts: 82
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


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.