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=
0Record 2:
attribute_id=1
subject_id=2
sort_order=
0In 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)")