-->
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: HQL join returns Objects
PostPosted: Fri Jul 09, 2010 7:36 am 
Newbie

Joined: Fri Jul 02, 2010 9:00 am
Posts: 9
Hi, I want to retrieve Text objects from my DB using an HQL join query.

This query returns Text objects

Code:
from Text text
join fetch text.role
where text.language = "ENGLISH"



This query returns Object objects.

Code:
from Text text
join text.role
where text.language = "ENGLISH"


Do you know why? This query is simpler than the one I really need. I was just wondering why adding "fetch" to the query results in Text objects. I need Text objects in the end, but I don't want to use fetch, because I don't need the "role".


Top
 Profile  
 
 Post subject: Re: HQL join returns Objects
PostPosted: Fri Jul 09, 2010 7:49 am 
Newbie

Joined: Fri Jul 02, 2010 9:00 am
Posts: 9
LOL. I keep answering my own questions here. But hey, at least it may be useful for other people.

When joining without using "fetch", Hibernate does not return the objects you want directly, it returns an array of objects.


Code:
String query = "from Text text " +
  "join text.role " +
  "where text.language = :language";

  Query q = session.createQuery(query)
  .setString("language", "FRENCH");
      
  List resultList = q.list();
 
  Iterator pairsIt = resultList.iterator();
  while(pairsIt.hasNext()){
     Object[] pair = (Object[]) pairsIt.next();
     System.out.println(pair[0]);
     System.out.println(pair[1] + "\n");
  }



/ * The above code prints

my.package.Text@cafb56
my.package.Role@1a7ddcf

my.package.Text@1d36dfe
my.package.Role@1a7ddcf

my.package.Text@186c730
my.package.Role@1a7ddcf

// etc...
*/   


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.