Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: null list elements when @OrderColumn has gaps in sequence
PostPosted: Sat Jan 07, 2012 3:44 am 
Newbie

Joined: Sat Jan 07, 2012 3:26 am
Posts: 3
Hello,

I have a "category tree" model that looks like this:

Code:
@Entity
@Table(name="CATEGORY")
public class Category {
    @Id @GeneratedValue @Column(name="ID")
    private long id;
   
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="PARENT_ID",nullable=true)
    private Category parent;
   
    @OneToMany(mappedBy="parent")
    @OrderColumn(name="CATEGORY_ORDER",nullable=false,insertable=true,updatable=true)
    private List<Category> children;


I bumped into an issue where the "children" list occasionally contains null elements. I was able to pinpoint the cause...gaps in the CATEGORY_ORDER sequence. For example, if I have this in my database:

Code:
select ID, NAME, CATEGORY_ORDER from CATEGORY where PARENT_ID=48;
+----+----------------------+----------------+
| ID | NAME                 | CATEGORY_ORDER |
+----+----------------------+----------------+
| 56 | Auto Repairs         |              1 |
| 64 | Electric Vehicle     |              9 |
| 73 | Road-Side Assistance |             18 |
+----+----------------------+----------------+


I expected category.children in this case to be a List of 3 Category objects. But I'm actually getting a List with 19 elements! Positions 0, 2-8, 10-17 are all null. Only positions 1, 9, 18 are non-null. It matches up with CATEGORY_ORDER values, but I didn't expect Hibernate to "fill the gaps" like that.

1. Is this expected behavior, or is it a bug?

2. If it's expected, can this behavior be changed, i.e. is there a property I can set that will instruct Hibernate not to fill in the gaps with nulls?

My desired behavior is -- disregard the actual order column values, using them only for sorting...not to imply list size.

Any other advice? I'm trying to avoid having to go through my database and update all of these order column values in a starting-from-zero sequence.

Thanks!

_________________
Dan Checkoway
first initial last name at gmail


Last edited by dcheckoway on Sat Jan 07, 2012 4:03 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: null list elements when @OrderColumn has gaps in sequence
PostPosted: Sat Jan 07, 2012 3:59 am 
Newbie

Joined: Sat Jan 07, 2012 3:26 am
Posts: 3
As a follow-on to this same issue...negative values in the order column are causing exceptions:

Code:
select ID, NAME, CATEGORY_ORDER from CATEGORY where PARENT_ID=409;
+----+-------+----------------+
| ID | NAME  | CATEGORY_ORDER |
+----+-------+----------------+
| 39 | Music |             -1 |
| 52 | Humor |              3 |
+----+-------+----------------+


Code:
Category category = em.find(Category.class, 409L);
System.out.println(category.getChildren().size());


Code:
java.lang.ArrayIndexOutOfBoundsException: -1
        at java.util.ArrayList.set(ArrayList.java:339)
        at org.hibernate.collection.PersistentList.readFrom(PersistentList.java:409)
        at org.hibernate.loader.Loader.readCollectionElement(Loader.java:1156)
        at org.hibernate.loader.Loader.readCollectionElements(Loader.java:774)
        at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:622)
        at org.hibernate.loader.Loader.doQuery(Loader.java:829)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
        at org.hibernate.loader.Loader.loadCollection(Loader.java:2166)
        at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
        at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627)
        at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
        at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1863)
        at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:369)
        at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
        at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:134)
        at org.hibernate.collection.PersistentList.size(PersistentList.java:114)
        at com.mycompany.test.TestCategoryChildren.testCategoryChildren(TestCategoryChildren.java:35)

_________________
Dan Checkoway
first initial last name at gmail


Top
 Profile  
 
 Post subject: Re: null list elements when @OrderColumn has gaps in sequence
PostPosted: Tue Apr 10, 2012 6:05 pm 
Newbie

Joined: Mon Feb 20, 2012 10:51 am
Posts: 2
Very interested in an answer to this question as well.


Top
 Profile  
 
 Post subject: Re: null list elements when @OrderColumn has gaps in sequence
PostPosted: Tue Apr 10, 2012 6:08 pm 
Newbie

Joined: Sat Jan 07, 2012 3:26 am
Posts: 3
Bumparino...any hibernate devs out there listening? Any advice on this?

_________________
Dan Checkoway
first initial last name at gmail


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.