@OrderColumn, @OneToMany & null index column for collection

前端 未结 5 440
梦谈多话
梦谈多话 2020-12-30 09:03

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 cl         


        
5条回答
  •  伪装坚强ぢ
    2020-12-30 09:45

    Do something like this:

    @Entity
    class Parent {
    
        @OneToMany
        @IndexColumn(name = "index_column")
        List children;
    }
    
    @Entity
    class Child {
    
        @ManyToOne
        Parent parent;
        @Column(name = "index_column")
        Integer index;
    
        @PrePersist
        @PreUpdate
        private void prepareIndex() {
            if (parent != null) {
                index = parent.children.indexOf(this);
            }
        }
    }
    

提交回复
热议问题