12

I am trying to create parent child tables where the order is preserved. The example 7.8 from Hibernate documentation shows how to do this:

@Entity
public class Customer {
   @Id @GeneratedValue public Integer getId() { return id; }
   public void setId(Integer id) { this.id = id; }
   private Integer id;

   @OneToMany(mappedBy="customer")
   @OrderColumn(name="orders_index")
   public List<Order> getOrders() { return orders; }
   public void setOrders(List<Order> orders) { this.orders = orders; }
   private List<Order> orders;
}

@Entity
public class Order {
   @Id @GeneratedValue public Integer getId() { return id; }
   public void setId(Integer id) { this.id = id; }
   private Integer id;

   public String getNumber() { return number; }
   public void setNumber(String number) { this.number = number; }
   private String number;

   @ManyToOne
   public Customer getCustomer() { return customer; }
   public void setCustomer(Customer customer) { this.customer = customer; }
   private Customer number;
}

from http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/collections.html#collections-indexed

When I try this I get an error: null index column for collections

There is Hibernate issue that describes the problem and gives an invalid example, but it specifically says that the example I gave above from the docs IS valid.

@Entity
public class Parent {
    @OneToMany(mappedBy="parent")
    @OrderColumn(name="order")
    private List<Child> children;
}

@Entity
public class Child {
    @ManyToOne
    private Parent parent;
}

from: https://hibernate.onjira.com/browse/HHH-5390

Maybe I'm being dense, but I don't see the difference between these two examples. One is:

@OneToMany(mappedBy="customer")
@OrderColumn(name="orders_index")

The other is:

@OneToMany(mappedBy="parent")
@OrderColumn(name="order")

And of course, I haven't figured out how to the get OrderColumn to work. Does anyone have any insight into why one of these examples is valid and the other is not?

Mark
  • 1,228
  • 1
  • 10
  • 18

5 Answers5

8

The bug refers to Hibernate 3.5.3 while documentation refers to Hibernate 3.6. It is my understanding from comments that the issue HHH-5390 has been resolved. Which version of Hibernate do you use? Note that you must have a column with exact specified name in @OrderCoulumn.

Also see this discussion about that same issue and a workaround in case of 3.5.


Update

Apparently it remains unsupported and there is a documentation bug as described by HHH-5732. I thought from HHH-5390 that the person who it was assigned (same who owns HHH-5390) has agreed to fix it. But it's not clear whether and when it is going to happen.

Selaron
  • 6,105
  • 4
  • 31
  • 39
Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
  • I'm on Hibernate 3.6.0. When I try to follow the example, I get the error, "null index column for collections". The jira issue says it is for 3.5.3 and that it will not be fixed, so I don't think it is a bug that was fixed in 3.6. The commentary on the bug says that what is shown for the bug is invalid but what is shown in the documentation is similar but valid. – Mark Jul 25 '11 at 14:42
  • Apparently it's a documentation bug and it's still unsupported. See my update. – Alex Gitelman Jul 25 '11 at 15:42
  • Thanks. It appears it may be possible to achieve the same thing using the examples here: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-collection-extratype-indexbidir – Mark Jul 25 '11 at 19:52
  • Hello. I have the same problem when using Maps. I don't have any @OrderColumn. And I still get this error .. I made my OneToMany unidirectional and it works. (ps: in EclipseLink works well from the beginning) – Cosmin Cosmin May 23 '12 at 13:53
4

For me the point was to set the column declared in @OrderColumn to the NOT NULL and with default value 0

2

Do something like this:

@Entity
class Parent {

    @OneToMany
    @IndexColumn(name = "index_column")
    List<Child> children;
}

@Entity
class Child {

    @ManyToOne
    Parent parent;
    @Column(name = "index_column")
    Integer index;

    @PrePersist
    @PreUpdate
    private void prepareIndex() {
        if (parent != null) {
            index = parent.children.indexOf(this);
        }
    }
}
Daniel De León
  • 13,196
  • 5
  • 87
  • 72
2

Moin,

the same problem occurs on hibernate.core 5.1.4 final. Using Set and HashSet on tag @OrderColumn (like Augustin said) fix the problem.

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
0

Maybe these can help you:

I have the same problem with an old version of hibernate (3.5.6) with tag @IndexColumn and find one good non-invasive workaround: try to change your List<Message> to Set<Message> Object and use HashSet instead of ArrayList. It's seems that old Hibernate versions work better with Sets.

Good luck!