-->
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: How to get Hibernate to manage a sort order...
PostPosted: Tue Jun 22, 2004 2:20 am 
Newbie

Joined: Thu Dec 18, 2003 12:30 am
Posts: 9
Using: Hibernate v2.1

I have a class, X, whose objects maintain lists of child objects of the same type, X (x1.getChildren() returns List[x2, x3, ..., xn]). I have hibernate set up to maintain the sort order for the children (via the <index> sub-element to the <list>). However, since I obtain the top level X's via a query, Hibernate does not maintain the sort-order at this level.

My first thought was to just introduce a new class, Y, such that Y would have the top level X's as it's children. Unfortunately, this doesn't work because there's no table to map to Y.

My question is: Is there a way to do what I've described without the introduction of the 'dummy' class?

I can't provide actual code snippets, so I stripped out unnecessary stuff and changed names...

DB table:

Code:
X
(
  xId VARCHAR(32) NOT NULL PRIMARY KEY,
  parentId VARCHAR(32) REFERENCES X (xId),
  sortOrder INTEGER NOT NULL
)


Class: (This code, as presented, is buggy but I didn't want extraneous details of bounds checking to get in the way)
Code:
public class X
{
  private String id;
  private X parent;
  private List children = new LinkedList();

  public void addChild(X child)
  {
    child.setParent(this);
    children.add(child);
  }

  public void bumpChild(X child, int delta)
  {
    int oldIndex = children.indexOf(child);
    children.add(child, oldIndex + delta);
  }

  // appropriate getters/setters
}


Relevant portion of the Hibernate Mapping:
Code:
<class name="X" table="X">
  <id name="id">
    <generator class="uuid.hex"/>
  </id>

  <many-to-one class="X" name="parent" colum="parentId">
  <list name="children" table="X" cascade="all" inverse="true">
    <key="parentId"/>
    <index column="sortOrder"/>
    <one-to-many class="X"/>
  </list>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 22, 2004 2:45 am 
Beginner
Beginner

Joined: Mon Jun 21, 2004 7:59 pm
Posts: 21
Don't know if it works for top level collections, but give this a try.

sortedReturn = s.filter( Query.list(), "order by this.name" );

Not automatic, but it's quick.

You could always implement a comparitor and use the JDK Collections.sort() method as well.

Hope this helps.

-Jim


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 22, 2004 5:14 am 
Newbie

Joined: Thu Dec 18, 2003 12:30 am
Posts: 9
Thanks Jim, but this isn't the problem I'm having. I can get them to come out of the database in the proper order if I maintain the sort order column directly. I want Hibernate to manage that column (it does for the child lists).

Actually, I've come up with a work-a-round that I can live with. Instead of introducing a dummy class, I introduced a 'root' object. Basically, I have an object that the app never sees (let's call it x0). All the 'top level' objects (x1, x2, x3) are actually it's children. When anything in my app calls for the top level objects, what I do is load up the root object and return the list of it's children. The app doesn't know the difference, and, when it re-sorts the list, hibernate maintains the sort-order in the database for me just as it does for the actual child objects (x11, x12, ... ) :)

Thanks for your help, but I'm gonna give up on finding the best solution since I have one that works and is just as efficient.


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.