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: @OrderBy: how to add new elements to the collection?
PostPosted: Wed Sep 09, 2009 2:35 pm 
Newbie

Joined: Wed Feb 25, 2009 8:48 am
Posts: 5
Hi all,

I have an association with @OrderBy:

Code:
  @ManyToMany
  @OrderBy("weight DESC")
  private final Collection<PropertyDefinitionImpl> propertyDefinitions = new ArrayList<PropertyDefinitionImpl>();

I am not sure how to add elements to this collection in order to have the weight invariant maintained:

Code:
  public PropertiesDefiner addPropertyDefinition(PropertyDefinition newProperty)
  {
    if (newProperty == null) throw new IllegalArgumentException("Null property definition.");

    propertyDefinitions.add((PropertyDefinitionImpl) newProperty); // THIS IS NOT ENOUGH, RIGHT?

    return this;
  }


Am I right in assuming that it is my and not Hibernate's responsibility to insert the new element at the correct position in the collection?

When the entity is transient, propertyDefinitions is an ArrayList and it's obviously up to me to insert the new element at the right place. But when the entity is loaded from the DB, propertyDefinitions is a proxy, maybe not yet fully loaded due to lazy loading. What is the recommended way to add the new element without forcing Hibernate to load all elements of the collection?

I observed that when I run the above code and the entity is loaded from the DB, the PersistedBag just appends the new element to the end, ignoring the @OrderBy at all. – Does @OrderBy only concern loading?

Thanks,
Kaspar


Top
 Profile  
 
 Post subject: Re: @OrderBy: how to add new elements to the collection?
PostPosted: Wed Sep 09, 2009 2:46 pm 
Newbie

Joined: Wed Oct 01, 2008 2:27 pm
Posts: 18
hbf wrote:
Does @OrderBy only concern loading?


Seems so...

How about making PropertyDefinitionImpl Comparable and using a TreeSet rather than an ArrayList?


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.