Let's say, we have an @OneToMany relation for entities A and B. Each A can have many B.
@Entity
public class A{
@Id
private long id;
...
@OneToMany(mappedBy="ref")
private List<B> items;
...
}
@Entity
public class B{
@Id
private long id;
@ManyToOne
@JoinColumn(name = "A_ID")
private A ref;
}
This application is in production for some time and the order of the Bs saved into db was not relevant till now.
However, now the exact order of the Bs is required. For this, I have added the @OrderColumn like below.
@OneToMany
@OrderColumn(name="B_ORDER")
private List<B> items;
It works fine and the B table has now the B_ORDER column with order values.
Now the production db needs to be updated. A new column B_ORDER has been added to the table B. However, for existing rows, this column has no value (null). When you try to retrieve a list of Bs for already existing record, I get the following exception:
org.hibernate.HibernateException: null index column for collection
It is Ok to have a list of Bs in any order for old records. @OrderColumn has the property "nullable" which is by default true. But it does not help.
Is there a JPA way for this or do I have to fill those null values in db myself?
Using JPA 2.1 with Hibernate 4.3.7.