-->
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.  [ 4 posts ] 
Author Message
 Post subject: PersistentIdentifierBag duplicate items
PostPosted: Thu Oct 11, 2007 9:06 am 
Beginner
Beginner

Joined: Fri Oct 06, 2006 2:49 am
Posts: 25
Hi All,

I am facing something that I suspect to be a bug. I have mapped a collection as following:


Code:
    @CollectionOfElements(fetch = FetchType.EAGER)
    @Cascade(value = CascadeType.ALL)
    @JoinTable(name = "person_address", joinColumns = @JoinColumn(name = "person_id", nullable = false))
    @SequenceGenerator(name = "MySequence", sequenceName = "person_address_seq", allocationSize = 1)
    @CollectionId(columns = @Column(name = "collection_id"), type = @org.hibernate.annotations.Type(type = "long"), generator = "MySequence")
    private Collection<Address> addresses = new ArrayList<Address>();


In my queries, I returns duplicate adresses . It seems that for each row returns in the resultset, the address is added even if it has the same collectionId of an address already added.

If we look at the source code we can see that indead the element read are added to the field persistentIdentifierBag.values without taking care of the duplicates.

Code:
public class PersistentIdentifierBag {

protected List values; //element
protected Map identifiers; //index -> id
...

public Object readFrom(
      ResultSet rs,
      CollectionPersister persister,
      CollectionAliases descriptor,
      Object owner)
      throws HibernateException, SQLException {

      Object element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() );
      Object old = identifiers.put(
         new Integer( values.size() ),
         persister.readIdentifier( rs, descriptor.getSuffixedIdentifierAlias(), getSession() )
      );
      if ( old==null ) values.add(element); //maintain correct duplication if loaded in a cartesian product
      return element;
   }
}


I am wondering if it is normal that elements with the same collectionId can be contained in an persistentIdentifierBag ?

It will be grateful if anybody could enlight me ?

After reflexion, I am getting really the impression that it is a bug because if we consider that as normal behavior, it means that it should have the same semantic as bags. Therefore we should have the same limitations. I mean we should not be able to fetch simultaneously multiple bags in a query (what i am doing) -> an exception must be thrown.

Thx,
Tiggy


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 26, 2007 5:06 am 
Beginner
Beginner

Joined: Tue Jan 18, 2005 9:37 am
Posts: 29
Location: The Netherlands
Hi Tiggy,

I had the same problem. I think that it is caused by the fact that eager fetching makes a join and is unable to normalize the results of the join.

A workaround is to change EAGER into LAZY.

Regards,
Bas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 26, 2007 5:55 am 
Beginner
Beginner

Joined: Tue Jan 18, 2005 9:37 am
Posts: 29
Location: The Netherlands
Just found out that this was a hibernate design decision.
You need to filter the duplicates manually.
For example by using a LinkedHashSet

See the advanced Faqs for more..


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 26, 2007 7:38 am 
Beginner
Beginner

Joined: Fri Oct 06, 2006 2:49 am
Posts: 25
Hi,

Thanks a lot for your reply.
Indeed If I put my field in LAZY the join will not be done and the problem is resolved. At least until you don't fetch your collection in a query :(

If it is a design decision to allow duplicate in the list, I am kind of surprise because I don't see any significant advantage to use an identifierBag instead of a simple Bag.

Anyway I have made the decision to avoid using this annotation and replace it with an @IndexColumn. It works perfectly in this case because it sets the object at the index specified in the IndexColumn.

Tiggy


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