Java排序集合与数据库获取集合

时间:2019-03-08 11:16:53

标签: java string hibernate sorting

我在尝试对java中的对象列表进行排序时遇到问题。手动创建对象列表或从数据库中获取对象列表时,会得到不同的结果。

这是对象:

public class CompanyRoleProperties implements Serializable {

     private String officeName;

    public CompanyRoleProperties() {
        super();
    }

    public CompanyRoleProperties(String officeName) {
        super();
        this.officeName = officeName;
    }

    public static SQLQuery createQuery(final Session session, final StringBuilder query) {
        final SQLQuery sqlQuery = session.createSQLQuery(query.toString());

         sqlQuery.addScalar("officeName", StandardBasicTypes.STRING);
         sqlQuery.setResultTransformer(Transformers.aliasToBean(CompanyRoleProperties.class));
        return sqlQuery;
    }

.... 
}

我有一个函数,可以从数据库中检索数据并创建这些对象的列表:

public List<CompanyRoleProperties> getServiceProviders() {

    final StringBuilder query = new StringBuilder(); 
    query.append("select distinct * from getCompanyRoleProperties()");
    query.append(" ORDER BY startDate DESC , endDate DESC ");
    this.log.info(query.toString());

    return CompanyRoleProperties.createQuery(this.getSessionFactory().getCurrentSession(), query).list();

}

我有一个用于分类的通用比较器:

public class GenericComparator<T extends Object> implements Comparator<T> {
    private SortOrder   sortType    = null;
    private String      sortField   = null;

    public GenericComparator(String sortField, SortOrder sortType) {
        this.sortType = sortType;
        this.sortField = sortField;
    }

    @SuppressWarnings("unchecked")
    @Override
    public int compare(T o1, T o2) {
        try {
            Comparable data1 = null;
            try {
                data1 = (Comparable) PropertyUtils.getProperty(o1, sortField);
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                data1 = null;
            }

            Comparable data2 = null;
            try {
                data2 = (Comparable) PropertyUtils.getProperty(o2, sortField);
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                data2 = null;
            }

            return ObjectUtils.compare(data1, data2) * (sortType.equals(SortOrder.ASC) ? 1 : -1);
        } catch (final Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }


    public static <T extends Object> void sortList(List<T> list, String sortField, SortOrder sortType) {
        final GenericComparator<T> comparator = new GenericComparator<T>(sortField, sortType);
        Collections.sort(list, comparator);
    }


}

这是我使用的测试:

final String sortOrderProperty = "officeName";

List<CompanyRoleProperties> props = myPropertiesMapper.getServiceProviders();
System.err.println("Before DB : " + props);
props.forEach(item -> System.err.println(item));

GenericComparator.sortList(props, sortOrderProperty, SortOrder.ASC);
System.err.println("After DB : " + props);
props.forEach(item -> System.err.println(item));

List<CompanyRoleProperties> names = new ArrayList<>();
names.add(new CompanyRoleProperties("KBL European Private Bankers S.A."));
names.add(new CompanyRoleProperties("BDO Audit, S.A."));
System.err.println("Manual Before : " + names);
names.forEach(item -> System.err.println(item));
GenericComparator.sortList(names, sortOrderProperty, SortOrder.ASC);

System.err.println("Manual After : " + names);
names.forEach(item -> System.err.println(item));

这给了我以下输出:

Before DB : [CompanyRoleProperties [officeName=KBL European Private Bankers S.A. ], CompanyRoleProperties [officeName=BDO Audit, S.A.]]
CompanyRoleProperties [officeName=KBL European Private Bankers S.A. ]
CompanyRoleProperties [officeName=BDO Audit, S.A.]


After DB : [CompanyRoleProperties [officeName=KBL European Private Bankers S.A. ], CompanyRoleProperties [officeName=BDO Audit, S.A.]]
CompanyRoleProperties [officeName=KBL European Private Bankers S.A. ]
CompanyRoleProperties [officeName=BDO Audit, S.A.]



Manual Before : [CompanyRoleProperties [officeName=KBL European Private Bankers S.A., ], CompanyRoleProperties [officeName=BDO Audit, S.A., ]]
CompanyRoleProperties [officeName=KBL European Private Bankers S.A., ]
CompanyRoleProperties [officeName=BDO Audit, S.A., ]


Manual After : [CompanyRoleProperties [officeName=BDO Audit, S.A., ], CompanyRoleProperties [officeName=KBL European Private Bankers S.A., ]]
CompanyRoleProperties [officeName=BDO Audit, S.A., ]
CompanyRoleProperties [officeName=KBL European Private Bankers S.A., ]

如您所见,手动创建的列表已按预期排序,但是对数据库一没有影响。为什么会这样?

0 个答案:

没有答案