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