英文:
JPA @OrderBy with Hibernate
问题
I'm here to help with the translation. Here's the translated code part:
我正在使用Hibernate,并想要使用`@OrderBy`来对`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
}
If you have any more questions or need further assistance, please let me know.
英文:
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?
答案1
得分: 3
根据@OrderBy
的文档:
点号(".")符号用于引用嵌入属性中的属性。与点号符号一起使用的每个标识符的值都是相应嵌入字段或属性的名称。
示例:
@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;
}
所以,对于您的情况,您不能使用它。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论