-->
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: @IndexColum works incorrectly for duplicated values in DB
PostPosted: Thu Jul 14, 2011 11:49 am 
Newbie

Joined: Thu Jul 14, 2011 11:36 am
Posts: 9
Hi,

Hibernate 3.5.6. I have the following definitions
Code:
    @ElementCollection(targetClass=String.class, fetch=FetchType.LAZY)
    @JoinTable(name="subject_attribute",joinColumns=@JoinColumn(name="attribute_id"))
    @Column(name="subject_id")
    @IndexColumn(name="sort_order")
    public List<String> getParentsIds() {
        return parentsIds;
    }


The application is connected to a legacy DB. This DB has same values in field "sort_order" for the same "attribute_id". Like
Record 1:
attribute_id=1
subject_id=1
sort_order=0

Record 2:
attribute_id=1
subject_id=2
sort_order=0

In this case call getParentsIds() returns only ONE value instead of 2.

It seems to me that the reason of the issue is an error in org.hibernate.collection.PersistentList.java
Code:
   public Object readFrom(ResultSet rs, CollectionPersister persister, CollectionAliases descriptor, Object owner)
   throws HibernateException, SQLException {
      Object element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() ) ;
      int index = ( (Integer) persister.readIndex( rs, descriptor.getSuffixedIndexAliases(), getSession() ) ).intValue();

      //pad with nulls from the current last element up to the new index
      for ( int i = list.size(); i<=index; i++) {
         list.add(i, null);
      }

      list.set(index, element);
      return element;
   }

This method does not suppose that IndexColumn can have same values, as a result it overrides the first value by the second instead of adding a new element into list.

I expect that in this case Hibernate should read 2 values, but set correct values for "sort_order" in case of update the collection.

Also Hibernate throws an exception with I use "base=1" (anything different than zero) in the definitions (ArrayIndexOutOfBoundsException in line "list.set(index, element)")


Top
 Profile  
 
 Post subject: Re: @IndexColum works incorrectly for duplicated values in DB
PostPosted: Mon Jul 18, 2011 2:42 pm 
Newbie

Joined: Thu Jul 14, 2011 11:36 am
Posts: 9
So, I had to modify org.hibernate.collection.PersistentList.java (Hibernate 3.5.6) by the following way:

Code:
//added block
   private ArrayList<Integer> ordering;
   public boolean endRead() {
      ordering = null;//release memory
      return super.endRead();
   }
//end added block

//modified method
public Object readFrom(ResultSet rs, CollectionPersister persister, CollectionAliases descriptor, Object owner) throws HibernateException, SQLException {
   Object element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() ) ;
   int index = ( (Integer) persister.readIndex( rs, descriptor.getSuffixedIndexAliases(), getSession() ) ).intValue();
   if (ordering==null) {
      ordering = new ArrayList<Integer>();
   }
   for (int i = 0; i < ordering.size(); i++) {
      if (ordering.get(i)>index) {
         //found
         list.add(i,element);
         ordering.add(i,index);
         return element;
      }
   }
   //new or the greatest
   list.add(element);
   ordering.add(index);
   return element;
}


Now it handles any cases (duplicated, missed, etc.)


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.