Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@OrderColumn, @OneToMany & null index column for collection

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?

like image 222
Mark Avatar asked Jul 20 '11 14:07

Mark


1 Answers

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.

like image 62
Alex Gitelman Avatar answered Sep 22 '22 21:09

Alex Gitelman