1

I have two collections, which by default are set to PersistentSet by hibernate. The issue is that under the hood hibernate casts them to HashSet, but I want them to be set to LinkedHashSet, as I want to preserve the order in which the elements are added.

@ElementCollection(fetch = FetchType.LAZY)
private Set<String> responsibilities;

@ElementCollection(fetch = FetchType.LAZY)
private Set<String> requirements;

I have tried a couple of things, but without success

public void setResponsibilities(LinkedHashSet<String> responsibilities) {
    this.responsibilities = responsibilities;
}

public void setRequirements(LinkedHashSet<String> requirements) {
    this.requirements = requirements;
}

The other thing I tried was:

@ElementCollection(fetch = FetchType.LAZY)
private Set<String> requirements = new LinkedHashSet<>();

1 Answers1

0

Please have a look here https://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#collections-sorted Seems that this can be done using SortedSet or just try using below solution:

@OrderBy("created DESC")
private Set<MyEntity> entities;