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?