Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java对集合进行排序_Java_Spring_Hibernate_Sorting_Collections - Fatal编程技术网

用Java对集合进行排序

用Java对集合进行排序,java,spring,hibernate,sorting,collections,Java,Spring,Hibernate,Sorting,Collections,使用的框架:Spring 使用的ORM:Hibernate 我有两节课 class BatchExceptionDetails{ ... private Set<BatchExceptionComments> batchExceptionComments; } class BatchExceptionComments implements Comparable<BatchExceptionComments>{ ... @Override public int c

使用的框架:Spring

使用的ORM:Hibernate

我有两节课

class BatchExceptionDetails{
...
private Set<BatchExceptionComments> batchExceptionComments;
}

class BatchExceptionComments implements Comparable<BatchExceptionComments>{
...
@Override
    public int compareTo(BatchExceptionComments o) {
        // TODO Auto-generated method stub

        return this.getAddedOn().compareTo(o.getAddedOn());
    }
}
类BatchExceptionDetails{
...
专用集批次例外注释;
}
类BatchExceptionComments实现了可比较的{
...
@凌驾
公共整数比较o(批次例外注释o){
//TODO自动生成的方法存根
返回这个.getAddedOn().compareTo(o.getAddedOn());
}
}
它们通过一对多映射进行映射

BatchExceptionDetails中有一组BatchExceptionComments

我想按日期把这套衣服分类。BatchExcpetionComment有一个java.util.Date类型的属性,即addedOn。我希望最新的评论是集合的第一个元素

我正在接收的集合未排序。你能告诉我哪里出了问题吗


提前感谢

Set
是一个接口,因此无法确定它是否可排序。您必须使用正确的实现,如。如果你想 强调它是一个排序集,您应该使用
SortedSet
界面<代码>树集实现
分类集


或者你可以使用一个
列表
,然后你可以使用
集合对它进行排序。排序

经过一些闲逛,我找到了一个解决方案

我刚刚宣布了这一套

private Set<BatchExceptionComments> batchExceptionComments;
private Set批处理例外注释;
我用Order By来排列布景,而不是使用Comparable

 <set name="batchExceptionComments" table="BATCH_EXCEPTION_COMMENTS" 
                inverse="true" fetch="select" lazy="false" order-by="commentId">
            <key>
                <column name="EXCEPTION_ID" not-null="true" />
            </key>
            <one-to-many class="com.beans.BatchExceptionComments" />
        </set>

我相信按Id订购会更好


另外,我使用的是hbm.xml而不是注释

您使用的是
分类数据集
?或其他类型的
集合
集合。排序(batchExceptionComments)@lore,仅适用于列表,不适用于集合。集合未排序。改为使用列表。不能只使用
Set
,因为
Set
是一个接口。您使用的是
Set
的什么实现?我可以使用其他集合而不是Set吗?我的假设是,如果我们使用Spring,那么我们就必须使用Set进行包含。问题是Hibernate要求将集合指定为接口类型。就实现类而言,它使用自己的类来支持延迟加载之类的事情。但是,将类型指定为
SortedSet
而不是
Set
应该可以工作。无需初始化它。Hibernate将使用自己的
SortedSet
实现自动初始化它。@RobbyCornelissen,我使用了SortedSet,在调用属性[combeans.BatchExceptionDetails.batchExceptionComments(预期类型=java.util.SortedSet)]的setter时收到异常IllegalArgumentException@RobbyCornelissen,我的setter方法是public void setBatchExceptionComments(SortedSet batchExceptionComments){this.batchExceptionComments=batchExceptionComments;}