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!