-->
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.  [ 9 posts ] 
Author Message
 Post subject: Extend java.util.TreeSet and hibernate
PostPosted: Thu Jan 20, 2011 8:52 am 
Newbie

Joined: Thu Jan 20, 2011 8:48 am
Posts: 6
Good afternoon everybody,
I was not able to understand what is the behaviour of this example class
public class Test {
Set<Security> set;
}

if I persist it after having create a set =

public class MaturityDateOrderedSet<T extends Security> extends TreeSet<T> {

/**
*
*/
private static final long serialVersionUID = 5156221242775201041L;

public MaturityDateOrderedSet() {
super(new MaturityDateComparator<T>());
// TODO Auto-generated constructor stub
}


}


when I load the data fro the database, will the set be a MaturityDateOrderedSet ???

Best Regards
Edmondo Porcu


Top
 Profile  
 
 Post subject: Re: Extend java.util.TreeSet and hibernate
PostPosted: Thu Jan 20, 2011 7:04 pm 
Newbie

Joined: Mon Jan 10, 2011 7:21 pm
Posts: 8
don't think so..to my knowledge, the only SortedSet implementation you can initialise your collection with is TreeSet..
Now, having said that, if natural ordering is not good for your requirement, you *should* be able to get the same SortedSet result by using the @Sort Hibernate annotation and providing a Comparator as in:

@OneToMany
@Sort(type=org.hibernate.annotations.SortType.COMPARATOR,
comparator=YourComparator.class)

Note, that, in case this is important to you, @Sort is a Hibernate annotation and *not* a JPA one..

Hope that helps,
- Savvas Andreas


Top
 Profile  
 
 Post subject: Re: Extend java.util.TreeSet and hibernate
PostPosted: Fri Jan 21, 2011 5:11 am 
Newbie

Joined: Thu Jan 20, 2011 8:48 am
Posts: 6
Thank you,

what you are saying is that I cannot persist my special set in hibernate?


Thanks
Edmondo


Top
 Profile  
 
 Post subject: Re: Extend java.util.TreeSet and hibernate
PostPosted: Sun Jan 23, 2011 7:08 am 
Newbie

Joined: Mon Jan 10, 2011 7:21 pm
Posts: 8
well, Hibernate won't persist any metadata related to the java type of the Collection you are using if that's what you are asking..it will only persist the Entities contained within your Collection.

now, at the java level if you instantiate you Collection with a HashSet you'll get back a HashSet and if you instantiate it with a TreeSet you'll get back a TreeSet. What I'm not sure about is whether you'll get back your own Collection implementation if you instantiate it with an instance of that class..you can give it a try and see. :)

but before doing that, are you entirely sure that the Sorting strategy you have implemented in your own SortedSet class cannot be encapsulated in a Comparator?

Regards,
- Savvas Andreas


Top
 Profile  
 
 Post subject: Re: Extend java.util.TreeSet and hibernate
PostPosted: Mon Jan 24, 2011 4:54 am 
Newbie

Joined: Thu Jan 20, 2011 8:48 am
Posts: 6
No, on the contrary:)
My extended collection is a sorted set which always uses a given comparator...

instead of embedding the comparable interface in the class itself, I want to have multiple possible comparator.


public class MaturityDateComparator<T extends Security> implements
Comparator<T>, Serializable {

/**
*
*/
private static final long serialVersionUID = -2825625270582341263L;

@Override
public int compare(T o1, T o2) {
Date m1 = o1.getMaturityDate();
Date m2 = o2.getMaturityDate();
int result = 0;
if (m1 != null && m2 != null)
result = GottwareCalendarHelper.compareOnlyDate(m1, m2);
return result;
}

}

so is hibernate able to restore this after persisting ?

Thanks


Top
 Profile  
 
 Post subject: Re: Extend java.util.TreeSet and hibernate
PostPosted: Mon Jan 24, 2011 6:26 pm 
Newbie

Joined: Mon Jan 10, 2011 7:21 pm
Posts: 8
Like I mentioned above, at the java level, Hibernate will use whatever Collection implementation you specify..to my knowledge, the only SortedSet implementation supported is TreeSet..at the database level Hibernate *doesn't* persist any metadata related to the java data structure you use.

If you need to apply different sort strategies depending on your specific use case, I'd suggest you sort your collection at the Repository level before your persistent object is returned to the caller.

Hope that helps,
- Savvas Andreas


Top
 Profile  
 
 Post subject: Re: Extend java.util.TreeSet and hibernate
PostPosted: Tue Jan 25, 2011 9:02 am 
Newbie

Joined: Thu Jan 20, 2011 8:48 am
Posts: 6
So the right way I have to implement a SortedSet is simply to implements the comparable interface inside the object and persist it is a treeSet?

That will grand, when collections are loaded, that order is maintained?

Thanks
Edmondo


Top
 Profile  
 
 Post subject: Re: Extend java.util.TreeSet and hibernate
PostPosted: Tue Jan 25, 2011 9:48 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The objects in the set doesn't have to implement the Comparable interface. Hibernate has support for you to specify your own Comparator implementation using the @Sort annotation. See http://docs.jboss.org/hibernate/core/3. ... ons-sorted for some examples. It's very similar to the code you posted in the original post, except that you don't need to subclass TreeSet. The second post by 'savvas.andreas' contains the solution.


Top
 Profile  
 
 Post subject: Re: Extend java.util.TreeSet and hibernate
PostPosted: Tue Jan 25, 2011 9:59 am 
Newbie

Joined: Thu Jan 20, 2011 8:48 am
Posts: 6
Thank you for your precious help. It looks like this was the solution I was looking for.

Is there a reason why the annotation is specifeid in the getter?

Thanks
Edmondo


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