Changing from Collection to SortedSet

。_饼干妹妹 提交于 2021-01-27 07:09:22

问题


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

@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. 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.

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...


回答1:


The error you are getting seems to be coming from the JPA metamodel. 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.

The issue is that JPA only allows the collection interfaces, 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.

In EclipseLink, you can use a SortedSet if you mark the mapping as EAGER. I think the metamodel error was fixed, try the latest release.




回答2:


SortedSet javadoc to the rescue:

All elements inserted into a sorted set must implement the Comparable interface (or be accepted by the specified comparator).

Almost certainly, Footnote does not implement Comparable



来源:https://stackoverflow.com/questions/10092672/changing-from-collection-to-sortedset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!