-->
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.  [ 7 posts ] 
Author Message
 Post subject: Relationship that is both ordered and rejects duplicates
PostPosted: Fri Mar 02, 2007 7:24 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,
I need my assocations in my POJOs to be both ordered and to reject duplicates. If an entity is attempted to be added to a association relationship which is already in the association relationship, I wish for it to be ignored.

The List interface helps me for the ordered part but does not for duplication rejection requirement.

I notice as well that hibernate will always use it's own implementation for the association i.e. PersistentBag anyway so there is not point me specifying a smart custom List implementation for the association.

So the only way I can think to reject duplicates is to put in some extra logic into the POJO? I don't want to have to do this, as I want to keep the POJOs as simple and lightweight as possible,
Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 02, 2007 7:43 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

The only type of Object used in relationships that is easily able (no code required) to reject duplicates is a java.util.Set, which by design does not allow duplicates, but i think it is not possible to enforce ordering (i have not attempted it).

But maybe you could attempt to create it as so:

Code:
private Set<Entity> mEntities = new LinkedHashSet<Entity>();

or

Code:
private Set<Entity> mEntities = new SortedSet<Entity>();

but i have only used HashSet (you might be able to use it in conjunction with an @OrderBy).

Cheers,

Andy

_________________
Rules are only there to be broken


Last edited by andydale on Fri Mar 02, 2007 12:20 pm, edited 3 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 02, 2007 7:57 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
just had a quick look over the documentation and SortedSet is suppoerted by hibernate.

look at section 6.6 here (http://www.hibernate.org/hib_docs/reference/en/html/collections.html) for further info

Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 09, 2007 12:44 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
andydale wrote:
just had a quick look over the documentation and SortedSet is suppoerted by hibernate.

look at section 6.6 here (http://www.hibernate.org/hib_docs/reference/en/html/collections.html) for further info

Cheers,

Andy

Thanks for getting back to me Andy and I agree that is one way of doing it. However then I loose all the index api's, i.e. the get(index i) etc. api's which are really handy. It doesn't look like hibernate facilitate you writing your own List implementation which is a drawback.
Anybody any ideas or comments?
Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 09, 2007 1:21 pm 
Newbie

Joined: Fri Mar 09, 2007 12:41 pm
Posts: 3
Keeping the POJO as simple as possible, there for only using the Java standard library. The solution is:

Code:
private List<Entity> list = new LinkedList<Entity>();

public void add(Entity entity) {
  if(list.contains(entity)) {
     return;
  }
  list.add(entity);
}



Or you could extract the code into a helper class, leaving only one line of code and no extra complexity in your POJO.

Code:

class CollectionHelper {
    public static <E> void addNoDuplicates(Collection<E> first, E ... second) {
        addNoDuplicates(first, Arrays.asList(second));
    }

    public static <E> void addNoDuplicates(Collection<E> first, Collection<E> second) {
        for(E object: second) {
            if(!first.contains(object)) {
                first.add(object);
            }
        }
    }
}

private List<Entity> list = new LinkedList<Entity>();

public void add(Entity entity) {
    CollectionHelper.addNoDuplicates(list, entity);
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 12, 2007 5:44 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Fireblaze wrote:
Keeping the POJO as simple as possible, there for only using the Java standard library. The solution is:

Code:
private List<Entity> list = new LinkedList<Entity>();

public void add(Entity entity) {
  if(list.contains(entity)) {
     return;
  }
  list.add(entity);
}



Or you could extract the code into a helper class, leaving only one line of code and no extra complexity in your POJO.

Code:

class CollectionHelper {
    public static <E> void addNoDuplicates(Collection<E> first, E ... second) {
        addNoDuplicates(first, Arrays.asList(second));
    }

    public static <E> void addNoDuplicates(Collection<E> first, Collection<E> second) {
        for(E object: second) {
            if(!first.contains(object)) {
                first.add(object);
            }
        }
    }
}

private List<Entity> list = new LinkedList<Entity>();

public void add(Entity entity) {
    CollectionHelper.addNoDuplicates(list, entity);
}

Hi,
Thanks for that, I thought about that and agree it will work.
It will just result in an explosion of APIs on my pojo as I must have an accessor and mutator for every association and if I now also have to extra addXXX() and removeXXX() methods it will result in an explosion of APIs as I have quite a lot of relationship associations.
It's a pity there isn't a
@NoDuplicates annotation.
Thanks for your comments.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 6:47 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Note that if you exclude duplicates, indexes does not make sense, that's why the Set API does not have the notion in the first place.

_________________
Emmanuel


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