emmanuel wrote:
silvaran wrote:
I do it manually.... can Hibernate handle this automatically?
I answered already in this very post :-)
OK maybe there's a misunderstanding... if I have the following two entities:
Code:
class Child {
@ManyToOne @JoinColumn( name="parent_id" ) private Parent parent;
@Basic @Column( name="sequence_num" ) private Integer sequence;
}
class Parent {
@Id @Column( name="parent_id" ) private Long id;
@OneToMany( mappedBy="parent" ) @IndexColumn( name="sequence_num", base=0 ) private List<Child> children = new LinkedList<Child>();
}
The question was... can hibernate populate "Child.sequence" automatically, rather than me having to shuffle the sequence numbers around when I insert a child?... a la:
Code:
class Parent {
public void insertChild( int index, Child child ) {
this.children.add( index, child );
child.setParent( this );
Iterator<Child> i = this.children.iterator();
for ( int i = 0; i <= index; ++i ) { i.next(); }
while ( i.hasNext() ) { i.next().setSequence( ++index ); }
}
}
The above code probably (maybe) has bugs, I just whipped it up (I use something different), but hopefully it illustrates why it would be nice if Hibernate supported this.
Oh and a bonus question... if I have a unique constraint on (parent_id, sequence_num), and I change the order of children, can hibernate order things correctly to avoid violating the constraint? (so if I have a0, b1, c2, and I want to swap a and b's sequence, it would take two steps: a1, b1, c2; and a1, b0, c2; or is this unreasonable for all but the simplest cases?)... right now I'm using a deferred constraint (Oracle).