@OrderColumn, @OneToMany & null index column for collection

前端 未结 5 441
梦谈多话
梦谈多话 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:43

    The bug refers to Hibernate 3.5.3 while documentation refers to Hibernate 3.6. It is my understanding from comments that the issue HHH-5390 has been resolved. Which version of Hibernate do you use? Note that you must have a column with exact specified name in @OrderCoulumn.

    Also see this discussion about that same issue and a workaround in case of 3.5.


    Update

    Apparently it remains unsupported and there is a documentation bug as described by HHH-5732. I thought from HHH-5390 that the person who it was assigned (same who owns HHH-5390) has agreed to fix it. But it's not clear whether and when it is going to happen.

    0 讨论(0)
  • 2020-12-30 09:45

    Do something like this:

    @Entity
    class Parent {
    
        @OneToMany
        @IndexColumn(name = "index_column")
        List<Child> 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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 09:53

    Maybe these can help you:

    I have the same problem with an old version of hibernate (3.5.6) with tag @IndexColumn and find one good non-invasive workaround: try to change your List<Message> to Set<Message> Object and use HashSet instead of ArrayList. It's seems that old Hibernate versions work better with Sets.

    Good luck!

    0 讨论(0)
  • 2020-12-30 09:54

    For me the point was to set the column declared in @OrderColumn to the NOT NULL and with default value 0

    0 讨论(0)
  • 2020-12-30 09:57

    Moin,

    the same problem occurs on hibernate.core 5.1.4 final. Using Set and HashSet on tag @OrderColumn (like Augustin said) fix the problem.

    0 讨论(0)
提交回复
热议问题