-->
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.  [ 4 posts ] 
Author Message
 Post subject: What query should I use for a linked list database table?
PostPosted: Mon Sep 17, 2007 4:19 pm 
Newbie

Joined: Wed Nov 29, 2006 6:55 pm
Posts: 10
I am interested in using Hibernate to query a table that is a Linked List. One row in my database represents a client. This client can have a parent client and so on. The Linked List relationship is a one to many. One parent can have many children, but one child can have only one parent. The parent may be null.

I really want to query the database with one clientID and then retrieve the client and all the children clients.

The query seems strait-forward for the reverse... Having a child and pulling all the parent records. I am confused on how to traverse the tree going the opposite direction.

Here is an example of the table that I would use:

Client Table:
clientID int
description String
parentID int (This field represents the parent client and may be null if there is no parent client)

Example Data:
Code:
ClientID      Description                                 ParentID
1             Fred's Hamburger Joint                      null
2             Fred's Hamburger Joint Swing                1
3             Fred's Hamburger Joing Grave                1
4             Lola's Swing Shift                          2
5             Fred's Grave Shift                          3



I would really like to do a search for clientID = 2 and get
both the Fred's Hamburger Joint Swing and Lola's Swing Shift or search with ClientID = 1 and get all records in this case.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 8:21 am 
Newbie

Joined: Fri Oct 28, 2005 4:30 am
Posts: 16
Hi,

If you have already a mapping for this table Client that works, this hql query should work

Code:
public List<Client> getClientsByIdOrParentId(Integer clientId) {
  String query = "select c from Client c where (clientId = :id or parentId = :id)";
  String paramName = "id";

  List<Client> clients = getHibernateTemplate().find(query, paramName, clientId);

  // check if clients is null or empty here

  return clients;
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 10:23 am 
Newbie

Joined: Wed Nov 29, 2006 6:55 pm
Posts: 10
Thanks for the query. That will work for clients at the 2nd level of the tree. But in some instances, the children will have children, and they may have children, etc... I don't believe this query would work in that case.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 11:00 am 
Newbie

Joined: Fri Oct 28, 2005 4:30 am
Posts: 16
OK I see, it's a kind of recursive sql query
Do you have the sql query already ?


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