简体   繁体   English

从 Collection 到 SortedSet

[英]Changing from Collection to SortedSet

I'm changing a Collection to a SortedSet because I need it to always be in the same consistent order that they were created in. I've changed my model property from我正在将 Collection 更改为 SortedSet,因为我需要它始终保持与创建它们时相同的一致顺序。我已将我的 model 属性从

@OneToMany(cascade = CascadeType.ALL, mappedBy = "contentId")
private Collection<Footnote> footnoteCollection;

to

@OneToMany(cascade = CascadeType.ALL, mappedBy = "contentId")
private SortedSet<Footnote> footnoteSortedSet;

and all relevant functions so Netbeans no longer shows any errors.以及所有相关功能,因此 Netbeans 不再显示任何错误。 When I run the app I get the error: Exception Description: Could not load the field named [footnoteSortedSet] on the class [class com.mysite.cmt.model.Content_]. Ensure there is a corresponding field with that name defined on the class.当我运行该应用程序时出现错误: Exception Description: Could not load the field named [footnoteSortedSet] on the class [class com.mysite.cmt.model.Content_]. Ensure there is a corresponding field with that name defined on the class. Exception Description: Could not load the field named [footnoteSortedSet] on the class [class com.mysite.cmt.model.Content_]. Ensure there is a corresponding field with that name defined on the class.

Since I've just changed this properly and relaunched my app I'm struggling to figure out why it's saying it's not set...因为我刚刚正确地改变了它并重新启动了我的应用程序,所以我正在努力弄清楚为什么它说它没有设置......

The error you are getting seems to be coming from the JPA metamodel.您收到的错误似乎来自 JPA 元模型。 I assume you are generating this in some way, if you don't use the metamodel in Criteria, then you don't need this and the error will go away.我假设您正在以某种方式生成它,如果您不在 Criteria 中使用元模型,那么您不需要它并且错误将 go 消失。

The issue is that JPA only allows the collection interfaces, Map, List, Set, Collection.问题是JPA只允许集合接口,Map,List,Set,Collection。 So, while you could use a SortedSet in your new instances, object read from the database will use a special lazy List implementation.因此,虽然您可以在新实例中使用 SortedSet,但从数据库读取的 object 将使用特殊的惰性 List 实现。

In EclipseLink, you can use a SortedSet if you mark the mapping as EAGER.在 EclipseLink 中,如果将映射标记为 EAGER,则可以使用 SortedSet。 I think the metamodel error was fixed, try the latest release.我认为元模型错误已修复,请尝试最新版本。

SortedSet javadoc to the rescue: SortedSet javadoc 来拯救:

All elements inserted into a sorted set must implement the Comparable interface (or be accepted by the specified comparator).插入有序集合的所有元素都必须实现 Comparable 接口(或被指定的比较器接受)。

Almost certainly, Footnote does not implement Comparable几乎可以肯定, Footnote没有实现Comparable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SortedSet或排序集合 - SortedSet or sorted Collection Java集合类型不匹配:无法从TreeSet转换为SortedSet - Java collection Type mismatch: cannot convert from TreeSet to SortedSet SortedSet :: removeAll(headSet)失败,但从headSet派生另一个集合成功。 为什么? - SortedSet::removeAll( headSet ) fails, but deriving another collection from headSet succeeds. Why? 通过将排序的集合添加到SortedSet来重新排序一个集合有多昂贵 - How expensive is it to resort a sorted collection by adding it to a SortedSet Hibernate ConstraintViolationException:无法删除子项的SortedSet集合 - Hibernate ConstraintViolationException: could not delete the SortedSet Collection of Child 如何从 SortedSet 子集<String> - How to subSet from SortedSet<String> 休眠:compareTo的Null异常,使用SortedSet包装子集合 - Hibernate: Null exception at compareTo using SortedSet to wrap sub collection 比较器是仅与SortedSet一起使用还是可以与Collection中的其他接口一起使用? - Is Comparator only used with SortedSet or can it be used with other interfaces in Collection? 如何在Java中将数据从双端队列传输到SortedSet - How to transfer data from a Deque to a SortedSet in Java 如何从数组添加到SortedSet项目? - How to add to SortedSet items from an Array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM