-->
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.  [ 3 posts ] 
Author Message
 Post subject: fetching 1-to-many in a single query
PostPosted: Tue Nov 11, 2003 5:41 am 
Newbie

Joined: Tue Oct 28, 2003 2:20 am
Posts: 9
Location: Bangalore, India
Hi,

I have a one-to-many relationship as described in [url]http://forum.hibernate.org/viewtopic.php?p=2177261#2177261
[/url]

I want to fetch all Departments for a location. I do this by

Code:
  public Collection findByLocation(String location){
    ArrayList departments = new ArrayList();
    Session session = null;
   
    try {
      session = sf.openSession();   
      Query q = session.createQuery("from Department as dep where dep.location = :location");
      q.setParameter("location",location);
      for(Iterator i = q.list().iterator();i.hasNext();) {
        departments.add((Department)i.next());
      }                 
    } catch (Exception e) {
      System.out.println("Error in findByLocation " + e.getMessage());
      e.printStackTrace();
    } finally {
      try {
        session.close();
      } catch (Exception e) {
        System.out.println("Hibernate Exception " + e.getMessage());
        e.printStackTrace();
      }             
    }

    return departments;
  }


This fires 2 queries on the database
- first query to fetch all departments for this location
- second query to fetch all employees for the above departments

How do I make hibernate join these to queries into one? When I try

Code:
Query q = session.createQuery("from Department as dep left outer join fetch dep.employees where dep.location = :location");


Hibernate returns duplicate rows (obviously)!

Even if I try
Code:
Query q = session.createQuery("select dep from Department as dep left outer join fetch dep.employees where dep.location = :location");


I cannot avoid duplicates.

What am I doing wrong?


Top
 Profile  
 
 Post subject: update
PostPosted: Tue Nov 11, 2003 5:42 am 
Newbie

Joined: Tue Oct 28, 2003 2:20 am
Posts: 9
Location: Bangalore, India
Sorry the last code segment should read

Code:
Query q = session.createQuery("select distinct dep from Department as dep left outer join fetch dep.employees where dep.location = :location");


Regards,
Ram.
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 6:25 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Dinstinct refer to the SQL distinct, so having duplicate deps when you fetch employee is the expected behavior.

Distinct your deps in your java code. With an order by dep.id you can do an easy Helper class

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.