-->
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: Persisting a SortedSet with dynamically-assigned Comparator
PostPosted: Tue Aug 24, 2004 2:30 am 
Newbie

Joined: Wed Aug 04, 2004 4:16 pm
Posts: 12
Location: UCSC
Hibernate version: 2.1

Mapping documents: TBD

Code between sessionFactory.openSession() and session.close(): N/A

Full stack trace of any exception that occurs: N/A

Name and version of the database you are using: N/A

Debug level Hibernate log excerpt: N/A

Hi,
I have a class (AttributeSet) that contains a SortedSet
(attributeValues) that contains Strings. The AttributeSet
class allows a Comparator class to be given as a constructor
arg, which results in the underlying attributeValues
using either the natural ordering of the Strings or the
Comparator-dictated ordering. I would very much like to
persist this class. It seems that because the Comparator
may or may not exist, that the "<set .... sort="X"> notation
will not work, so I should use an order-by approach, yes?
I cannot seem to find an example as to how to do this,
exactly -- and I'd also like to make sure that when I eventually
get around to retrieving an instance of this class from the DB
that the same Comparator (if it existed) is still used for future
additions to the set. In theory a SortedSet, when persisted and
retrieved from the DB, would result in a TreeSet with the same
comparator as the original SortedSet -- chapter 6 doesn't
seem to address that issue directly. I could certainly make
the Comparator a property of the AttributeSet class.
Anyway -- to get back to the first of these problems, I apparently
need to make a column in the AttributeSet table that indicates
the ordering of the one-to-many contents of the attributeValues.
So...how am I to "fill in" these values? It is not as though each
individual String stored in attributeValues has a property
by which I can order. Do I need to make another wrapper class
that holds an integer/String, then assign the integers an "index"
each time an add/remove is done?
One other possibility seems to start with a List collection, but
I do have Sets here, so I could use a List internally, then when
anyone inserts perform a manual compare-and-insert step.
I could do that, but given that I am still so new to Hibernate and
have been so confused already (I have not used any sort of
database before either, so that nomenclature is new also), I thought
it best if I asked about how normal people would approach this problem.

Thanks all,
Jennifer Bevan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 4:33 pm 
Newbie

Joined: Wed Aug 04, 2004 4:16 pm
Posts: 12
Location: UCSC
As an update, I did try to use a proxy class for my Comparators,
because then my mapping file can say:
<set .... sort=HibernateProxyComparator .. >
I am not entirely certain this should work, however, because
I took a look at the hibernate source code and it seems as though
a SortedSet is returned by instantiating:
new TreeSet(new HibernateProxyComparator()), which of
course won't create the real underlying Comparator
class. I did make HibernateProxyComparator persisted;
it saves the name of the class to use for comparisons,
so when *it* is recovered from the database, a usable
Comparator results. I can make an association between
these classes -- but I don't think it does any good, because
SortedSet uses only the "sort = X" value. I know the
"order-by" option is available but I don't think it will work
because the SortedSet contains a base java class (String),
not anything with a property column I can use to sort
with. Can Hibernate retrieve Comparators in any other
way, or are there plans to make this so?

Now, I think that I could also make a method that would follow
any retrieval of the sorted set from the database with a
create-proper-comparator -> make new set -> copy contents
sequence. I will if I have to, and in this particular case this
scalability problem is not a massive concern. It does seem as
though there should be or will be a better way, so I would
very much appreciate any feedback on this matter in the general
and my approach in the specific.

Thanks!
-Jen


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.