hibernate - Adding the JPA @OrderColumn afterwards causes null index column for collection error -
let's say, have @onetomany relation entities , b. each 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 ref; }
this application in production time , order of bs saved db not relevant till now.
however, exact order of bs required. this, have added @ordercolumn below.
@onetomany @ordercolumn(name="b_order") private list<b> items;
it works fine , b table has b_order column order values.
now production db needs updated. new column b_order has been added table b. however, existing rows, column has no value (null). when try retrieve list of bs existing record, following exception:
org.hibernate.hibernateexception: null index column collection
it ok have list of bs in order old records. @ordercolumn has property "nullable" default true. not help.
is there jpa way or have fill null values in db myself?
using jpa 2.1 hibernate 4.3.7.
the problem have jpa provider, in case hibernate, need a contiguous (non-sparse) ordering of values of order column.
however, when add new column new database, values null unless have defined default value of 0.
you might think adding default value trick. sorry, not case. if so, collection show max 1 item.
to solve problem, have add each collection sequential, non-sparse numbering.
in practice should alter entity temporary can access respective field:
@entity public class b{ @id private long id; @manytoone @joincolumn(name = "a_id") private ref; @column(name = "b_order") private integer order; }
now can load items. need next write temporal method loading items database numbers each item per collection starting 0 length of collection minus 1.
the above suggestion 1 way find convenient. there many ways solve it. manually if have enough time.
what should take away answer items of collection need numbered starting 0. otherwise hibernate wont load items collection. need fulfill constrains described here.
Comments
Post a Comment