Is @ManyToMany(mappedBy = … ) + @OrderColumn supported by the JPA?

我的梦境 提交于 2019-12-05 00:42:04

The javadoc or OrderColumn has the information you're looking for:

The OrderColumn annotation is specified on the side of the relationship that references the collection that is to be ordered. The order column is not visible as part of the state of the entity or embeddable class.

The OrderBy annotation should be used for ordering that is visible as persistent state and maintained by the application. The OrderBy annotation is not used when OrderColumn is specified.

OrderColumn is used to add an additional column to the join table, used to maintain the order of the list. The order column, as mentioned in the javadoc, is not part of the entity state. In your case, it seems you want to order the elements in the list by one of their persistent property. In that case, you must use the OrderBy annotation (or have a getter that sorts the list before returning it).

In OPENJPA there is a problem regarding @OrderColumn, the retrieve operation works, but saving an entity gets an error "detached object", this error is associated to adding this annotation (@OrderColumn)

The solution is to use comparator in the parent.getChildEntity class to order the child entity list, thus, this avoids using @OrderColumn.

Collections.sort(this.child, new Comparator<class_type>() {
@Override
        public int compare(class_type p1, class_type p2) {
            return (int) (p1.getId() - p2.getId());
        }

    });

Solved :) John V, Col

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!