JPA @OrderBy与Hibernate

huangapple go评论145阅读模式
英文:

JPA @OrderBy with Hibernate

问题

I'm here to help with the translation. Here's the translated code part:

  1. 我正在使用Hibernate并想要使用`@OrderBy`来对`resultSet`进行排序
  2. public class TopLevelEntity extends Entity {
  3. @OneToMany(mappedBy = "topLevelEntity", fetch = FetchType.LAZY,
  4. cascade = CascadeType.ALL, orphanRemoval = true)
  5. private Set<TopLevelEntityTranslation> translations;
  6. //other fields
  7. }
  8. public class NextLevelEntity extends Entity {
  9. @OneToMany(mappedBy = "nextLevelEntity", fetch = FetchType.LAZY,
  10. cascade = CascadeType.ALL, orphanRemoval = true)
  11. private Set<NextLevelEntityTranslation> translations;
  12. @Column
  13. private Long number;
  14. //other fields
  15. }
  16. public class TopLevelEntityTranslation extends Entity {
  17. @ManyToOne
  18. @JoinColumn(name = "TOP_LVL_ENTITY_ID")
  19. private TopLevelEntity topLevelEntity;
  20. @OrderBy("nextLevelEntity.number")
  21. @OneToMany(mappedBy = "topLevelEntityTranslation", fetch = FetchType.LAZY,
  22. cascade = CascadeType.ALL, orphanRemoval = true)
  23. private Set<NextLevelEntityTranslation> nextLevelEntityTranslations;
  24. //other fields
  25. }
  26. public class NextLevelEntityTranslation extends Entity {
  27. @ManyToOne
  28. @JoinColumn(name = "TOP_LVL_ENTITY_TR_ID")
  29. private TopLevelEntityTranslation topLevelEntityTranslation;
  30. @ManyToOne
  31. @JoinColumn(name = "NEXT_LVL_ENTITY_ID")
  32. private NextLevelEntity nextLevelEntity;
  33. //other fields
  34. }

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:

  1. public class TopLevelEntity extends Entity {
  2. @OneToMany(mappedBy = &quot;topLevelEntity&quot;, fetch = FetchType.LAZY,
  3. cascade = CascadeType.ALL, orphanRemoval = true)
  4. private Set&lt;TopLevelEntityTranslation&gt; translations;
  5. //other fields
  6. }
  7. public class NextLevelEntity extends Entity {
  8. @OneToMany(mappedBy = &quot;nextLevelEntity&quot;, fetch = FetchType.LAZY,
  9. cascade = CascadeType.ALL, orphanRemoval = true)
  10. private Set&lt;NextLevelEntityTranslation&gt; translations;
  11. @Column
  12. private Long number;
  13. //other fields
  14. }
  15. public class TopLevelEntityTranslation extends Entity {
  16. @ManyToOne
  17. @JoinColumn(name = &quot;TOP_LVL_ENTITY_ID&quot;)
  18. private TopLevelEntity topLevelEntity;
  19. @OrderBy(&quot;nextLevelEntity.number&quot;)
  20. @OneToMany(mappedBy = &quot;topLevelEntityTranslation&quot;, fetch = FetchType.LAZY,
  21. cascade = CascadeType.ALL, orphanRemoval = true)
  22. private Set&lt;NextLevelEntityTranslation&gt; nextLevelEntityTranslations;
  23. //other fields
  24. }
  25. public class NextLevelEntityTranslation extends Entity {
  26. @ManyToOne
  27. @JoinColumn(name = &quot;TOP_LVL_ENTITY_TR_ID&quot;)
  28. private TopLevelEntityTranslation topLevelEntityTranslation;
  29. @ManyToOne
  30. @JoinColumn(name = &quot;NEXT_LVL_ENTITY_ID&quot;)
  31. private NextLevelEntity nextLevelEntity;
  32. //other fields
  33. }

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文档

点号(".")符号用于引用嵌入属性中的属性。与点号符号一起使用的每个标识符的值都是相应嵌入字段或属性的名称。

示例:

  1. @Entity
  2. public class Person {
  3. ...
  4. @ElementCollection
  5. @OrderBy("zipcode.zip, zipcode.plusFour")
  6. public Set<Address> getResidences() {...};
  7. ...
  8. }
  9. @Embeddable
  10. public class Address {
  11. protected String street;
  12. protected String city;
  13. protected String state;
  14. @Embedded
  15. protected Zipcode zipcode;
  16. }
  17. @Embeddable
  18. public class Zipcode {
  19. protected String zip;
  20. protected String plusFour;
  21. }

所以,对于您的情况,您不能使用它。

英文:

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:

  1. @Entity
  2. public class Person {
  3. ...
  4. @ElementCollection
  5. @OrderBy(&quot;zipcode.zip, zipcode.plusFour&quot;)
  6. public Set&lt;Address&gt; getResidences() {...};
  7. ...
  8. }
  9. @Embeddable
  10. public class Address {
  11. protected String street;
  12. protected String city;
  13. protected String state;
  14. @Embedded
  15. protected Zipcode zipcode;
  16. }
  17. @Embeddable
  18. public class Zipcode {
  19. protected String zip;
  20. protected String plusFour;
  21. }

So, you can not use it for your case.

huangapple
  • 本文由 发表于 2020年8月3日 19:45:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63228831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定