1

We use Hibernate to save a list of entities using OrderColumn to maintain their order.

class MyObject {

...

   @OneToMany(mappedBy = "myobject", cascade = {CascadeType.PERSIST, CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
   @OrderColumn(name = "index_number")
       private List<sampleElement> sampleElement;
}

When saving the list of elements, the index_number starts with 1 while the documentation suggests it should start from 0. This also causes an issue upon reading because the read list then contains a null element at position 0.

We have looked around quite a bit and are somewhat out of ideas. Any suggestions?

1 Answer 1

1

It can be customized by using @ListIndexBase.

Defines the start index value for a list index as stored on the database. This base is subtracted from the incoming database value on reads to determine the List position; it is added to the List position index when writing to the database. By default list indexes are stored starting at zero.

  @OneToMany(mappedBy = "myobject", cascade = {CascadeType.PERSIST, CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
  @OrderColumn(name = "index_number")
  @ListIndexBase(1)
  private List<sampleElement> sampleElement;

See also this section of the hibernate documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.