Say I have a class Mother with a oneToMany mapping to Kittens
@Entity @org.hibernate.annotations.Entity(dynamicUpdate = true) @Table(name = "Mother") ..... @OneToMany(fetch = FetchType.LAZY, targetEntity=Kittens.class, cascade=CascadeType.ALL) @JoinColumn(name="motherId") private List<Kittens> kittens;
I am using the criteria Api to provide a list
Criteria criteria = this.getSession().createCriteria(Mother.class); Criterion MotherType = Restrictions.eq("type", "domesticated"); criteria.add(MotherType) .addOrder(Order.asc("motherName")) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) .setFetchMode("kittens", FetchMode.JOIN) // .addOrder(Order.asc("kittens.kittenName")); List test = criteria.list();
Works fine until I try to add the order of the kittens field kittenName
I've also tried adding the @OrderBy(value="kittenName") annotation under the Kitten @OneToMany(...etc ) which works fine until you use the criteria API and this order will precede any other order statements in the sql.
Cheers in advance for any help...
When a join table is used in mapping a relationship with an embeddable class on the owning side of the relationship, the containing entity rather than the embeddable class is considered the owner of the relationship. If the JoinTable annotation is missing, the default values of the annotation elements apply.
The @JoinColumn annotation helps us specify the column we'll use for joining an entity association or element collection. On the other hand, the mappedBy attribute is used to define the referencing side (non-owning side) of the relationship.
You need to add the @javax.persistence.OrderBy
annotation to your kittens
list.
@OneToMany(fetch = FetchType.LAZY, targetEntity=Kittens.class, cascade=CascadeType.ALL) @JoinColumn(name="motherId") @OrderBy("kittenName") private List<Kittens> kittens;
@See
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With