In OpenJPA, an entity class with an OrderColumn annotation on an
attribute of type java.util.Set is allowed even though the Set
interface does not guarantee order. EclipseLink is stricter and allows
an OrderColumn annotation only on attributes of type
java.util.List.
This rule flags @OrderColumn annotations found on attributes that implement
the following interfaces:
Although SortedSet, a subinterface of Set, provides ordering with its Comparable
interface, it is not supported by EclipseLink.
You can solve this issue in two ways depending on the importance of retaining order of the collection.
Set collection
to a List collection.OrderColumn annotation.
In the following entity class, both OrderColumn annotations are flagged by
this rule.
import java.util.HashSet; import java.util.Set; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OrderColumn; import javax.persistence.OneToMany; @Entity public class OrderColumnExampleEntity { @Id private int id; @ElementCollection @OrderColumn private Set<String> setOfStrings = new HashSet<String>(); @OneToMany @OrderColumn private Set<RelatedObject> setOfObjects = new HashSet<RelatedObject>(); } |
The following example shows implementing both possible solutions.
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OrderColumn; import javax.persistence.OneToMany; @Entity public class OrderColumnExampleEntity { @Id private int id; // Remove the OrderColumn annotation @ElementCollection private Set<String> setOfStrings = new HashSet<String>(); // Change the collection to a List @OneToMany @OrderColumn private List<RelatedObject> setOfObjects = new ArrayList<RelatedObject>(); } |
For information about this issue and other OpenJPA to EclipseLink migration issues, see the OpenJPA to EclipseLink JPA Migration: Mappings guide.