When I debug it appears that when using j.p.OrderBy the PersistentSet.set is a HashSet and not a LinkedHashSet. Here is my Animal and Country models just to make sure it is not some kind of dumb annotation problem on my end:
Animal trimmed getters and setters
Code:
@Entity
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@NamedQueries({
@NamedQuery(name=Animal.QUERY_GET_ALL_ANIMALS_AS_SELECT_ITEMS, query="select new javax.faces.model.SelectItem(a, a.name) from Animal a")
})
@SuppressWarnings("serial")
public class Animal implements Serializable {
public static final String QUERY_GET_ALL_ANIMALS_AS_SELECT_ITEMS = "getAllAnimalsAsSelectItems";
@Id @GeneratedValue
private Long id;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="CREATED", nullable = false, updatable = false)
private Date created = new Date();
@Column(unique=true, nullable=false)
private String name;
@Enumerated(EnumType.STRING)
private Gender gender;
@Column(length=4000)
private String description;
private String imageUrl;
@Column(nullable=false)
private double price;
private Date dateOfBirth;
@ManyToOne(fetch=FetchType.LAZY)
private Classification classification;
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="ANIMAL_COUNTRY",
joinColumns=@JoinColumn(name="ANIMAL_ID"),
inverseJoinColumns=@JoinColumn(name="COUNTRY_ID"))
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
@BatchSize(size=10)
@OrderBy("name")
private Set<Country> countries;
@SuppressWarnings("unused")
@Version
private Long version;
Country
Code:
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@SuppressWarnings("serial")
@BatchSize(size=25)
@NamedQueries({
@NamedQuery(name=Country.QUERY_ALL_COUNTRY_IDS_AS_SELECT_ITEMS, query="select new javax.faces.model.SelectItem(c.id, c.name) from Country c",
hints=@QueryHint(name="org.hibernate.cacheable", value="true")),
@NamedQuery(name=Country.QUERY_ALL_COUNTRIES_AS_SELECT_ITEMS, query="select new javax.faces.model.SelectItem(c, c.name) from Country c",
hints=@QueryHint(name="org.hibernate.cacheable", value="true"))
})
public class Country implements Serializable {
public static final String QUERY_ALL_COUNTRY_IDS_AS_SELECT_ITEMS = "allCountryIdsAsSelectItems";
public static final String QUERY_ALL_COUNTRIES_AS_SELECT_ITEMS = "allCountriesAsSelectItems";
@Id @GeneratedValue @Column(updatable=false)
private Long id; //NOPMD - wheelermm
@Column(unique=true, nullable=false)
public String name;
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
@ManyToMany(mappedBy="countries", fetch=FetchType.EAGER)
@Fetch(FetchMode.SUBSELECT)
@BatchSize(size=20)
public Set<Animal> animals;