Im using hibernate and want to use @OrderBy to order a resultSet:
public class TopLevelEntity extends Entity {
@OneToMany(mappedBy = "topLevelEntity", fetch = FetchType.LAZY,
cascade = CascadeType.ALL, orphanRemoval = true)
private Set<TopLevelEntityTranslation> translations;
//other fields
}
public class NextLevelEntity extends Entity {
@OneToMany(mappedBy = "nextLevelEntity", fetch = FetchType.LAZY,
cascade = CascadeType.ALL, orphanRemoval = true)
private Set<NextLevelEntityTranslation> translations;
@Column
private Long number;
//other fields
}
public class TopLevelEntityTranslation extends Entity {
@ManyToOne
@JoinColumn(name = "TOP_LVL_ENTITY_ID")
private TopLevelEntity topLevelEntity;
@OrderBy("nextLevelEntity.number")
@OneToMany(mappedBy = "topLevelEntityTranslation", fetch = FetchType.LAZY,
cascade = CascadeType.ALL, orphanRemoval = true)
private Set<NextLevelEntityTranslation> nextLevelEntityTranslations;
//other fields
}
public class NextLevelEntityTranslation extends Entity {
@ManyToOne
@JoinColumn(name = "TOP_LVL_ENTITY_TR_ID")
private TopLevelEntityTranslation topLevelEntityTranslation;
@ManyToOne
@JoinColumn(name = "NEXT_LVL_ENTITY_ID")
private NextLevelEntity nextLevelEntity;
//other fields
}
I have an entity hierarchy like shown above. But @OrderBy annotation(no the annotation ofc, but the statement generated by hibernate) throws an exception. Exception main part:
missing FROM-clause entry for table "nextLevelEntity"
There are few examples of using jpa @OrderBy but pattern of those that I found is the same with mine.
Read that @OrderBy doesn't work well with Hibernate's JPA implementation. But the question was asked 7 years ago. Is it true or i have a mistake in my code?
Actually, according to the documentation for the @OrderBy:
The dot (".") notation is used to refer to an attribute within an embedded attribute. The value of each identifier used with the dot notation is the name of the respective embedded field or property.
Example:
@Entity
public class Person {
...
@ElementCollection
@OrderBy("zipcode.zip, zipcode.plusFour")
public Set<Address> getResidences() {...};
...
}
@Embeddable
public class Address {
protected String street;
protected String city;
protected String state;
@Embedded
protected Zipcode zipcode;
}
@Embeddable
public class Zipcode {
protected String zip;
protected String plusFour;
}
So, you can not use it for your case.
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