MyEclipse+Hibernate 不按 ID 对属性集排序?
我将 Hibernate 与 Java 结合使用。
我有两个与外键关联的表。
Table: country Fields: ID, Name POJO class name : Country POJO class properties: id, name, cities Table: city Fields: ID, Name, CountryID POJO class name : Country
然后我使用MyEclipse的“hibernate逆向工程”。 它自动创建 DAO、抽象和 pojo 类。
一切都运转良好。 当我请求一个 Country 对象时,Hibernate 会检索它并用 CountryID 作为 Country.id 的城市填充属性“cities”。
仍然一切都很好,但是当我列出“城市”属性(java Set 类型),然后打印出所有城市的 ID 时,我得到了这个无序列表:
ID: 5 ID: 1 ID: 4 ID: 2
当我从 CountryDAO 类获取 Country 实例时,我应该在哪里编辑以获取按 ID 排序的城市?
I use Hibernate with Java.
I have two tables which are associated with foreign keys.
Table: country Fields: ID, Name POJO class name : Country POJO class properties: id, name, cities Table: city Fields: ID, Name, CountryID POJO class name : Country
Then I use "hibernate reverse engineering" of MyEclipse. It creates DAOs, abstracts and pojo classes automatically.
Everything works very well. When I request a Country object, Hibernate retrieves it and fills the property "cities" with cities which has CountryID as country.id.
Still everything fine but when I list "cities" property (java Set type), then print out IDs of all cities I got this unordered list:
ID: 5 ID: 1 ID: 4 ID: 2
Where should I edit to get cities ordered by ID when I obtain Country instances from the CountryDAO class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用 Hibernate 的 Criteria API 来提供这样的约束。 下面的部分将按 id 升序对帖子进行排序。
You may try to use Hibernate's Criteria API to provide such a constraint. Following piece is going to order the posts by id ascending.
您使用的是 XML 映射还是注释?
通常,映射集合上有一个“order-by”属性,可让您指定用于对集合进行排序的列以及升序或降序排序。
请参阅此页面:
http://docs。 jboss.org/hibernate/stable/core/reference/en/html/collections-mapping.html
搜索“order-by” - 您需要为您的示例找到适当的集合映射。
Are you using XML mapping, or annotations?
Usually, on mapped collections there is an "order-by" attribute that lets you specify a column to order the collection by, and asc or desc ordering.
See this page:
http://docs.jboss.org/hibernate/stable/core/reference/en/html/collections-mapping.html
Search for "order-by" - you'll need to find the appropriate collection mapping for your example.