0

如何对外键记录进行排序

如何对实体中包含的外键记录进行排序?

我有一对多的关系:一个组可以包含许多站点。

组.java

public class Group implements java.io.Serializable {

    private static final long serialVersionUID = -3698414318830180024L;

    private Collection<AppSite> appSites = new HashSet<AppSite>();

GroupDAO.java:

public class GroupDAO extends HibernateDaoSupport {

    @SuppressWarnings("unchecked")
    public Group getGroupById(Long id){
        Group group = getHibernateTemplate().get(Group.class, id);
        return group;
}

这是循环这些记录的jsp:

<p>Number of items: ${fn:length(groupCommand.group.appSites)}</p>

<ul>
    <c:forEach items="${groupCommand.group.appSites}" var="appSite">
        <li><a href="../appsites/appsite.page?appSiteId=${appSite.appSiteId}">${appSite.nameEng}</a></li>   
    </c:forEach>
</ul>

问题是 appSite.nameEng 没有正确排序。如何做到这一点?

到目前为止,我有这个 HQL:

from Group as g inner join g.appSites as s where g.appSiteGroupId=10 order by s.nameEng asc

在 Eclipse/Hibernate HQL 编辑器中,它返回适当数量的记录。但是,当我将 DAO 方法更改为此:

@SuppressWarnings("unchecked")
public Group getGroupById(Long id){
    //Group group = getHibernateTemplate().get(Group.class, id);
    Group group = (Group) getHibernateTemplate().find("from Group as g inner join g.appSites as s where g.appSiteGroupId=10 order by s.nameEng asc").get(0);
    return group;
}

我收到此错误:java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to org.domain.Group

更新#1

我有这个:

@SuppressWarnings("unchecked")
public Group getGroupById(Long id){
    //Group group = getHibernateTemplate().get(Group.class, id);
    List<Group> groupList= getHibernateTemplate().find("from Group as g inner join g.appSites as s where g.appSiteGroupId=10 order by s.nameEng asc");
    return (groupList.size() > 0 ) ? groupList.get(0) : null; //line 17
}

产生的错误:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to org.domain.Group
    org.dao.GroupDAO.getGroupById(GroupDAO.java:17)

更新#2

再试一次,同样的问题

组.java

public class Group implements java.io.Serializable {

    private Collection<AppSite> appSites = new ArrayList<AppSite>();

AppSiteComparator.java

public class AppSiteComparator implements Comparator<AppSite> {

    private String sortBy;

    public AppSiteComparator(String sortBy){
        this.sortBy = sortBy;
    }

    public AppSiteComparator(){
        this.sortBy = "nameEng";
    }

    @Override
    public int compare(AppSite a1, AppSite a2){
        if(this.sortBy.equals("nameEng"))
            return a1.getNameEng().compareTo(a2.getNameEng());
        else
            return 0;
    }
}

GroupDAO.java

@SuppressWarnings("unchecked")
public Group getGroupById(Long id){
    Group group = getHibernateTemplate().get(Group.class, id);
    Collections.sort((List<AppSite>) group.getAppSites(), new AppSiteComparator());
    return group;
}

错误

java.lang.ClassCastException: org.hibernate.collection.PersistentSet cannot be cast to java.util.List at org.dao.GroupDAO.getGroupById(GroupDAO.java:19)

更新#3

尝试使用标准:

Group group = (Group) session.createCriteria(Group.class)                   
    .createAlias("appSites", "a")
    .add(Restrictions.eq("appSiteGroupId",10L))
    .addOrder(Order.asc("a.nameEng")).list().get(0);

仍然无法正常工作......控制台中显示的订单:

Site name: test 4
Site name: Test eng
Site name: test 2
Site name: test 3

刚刚注意到... SQL 似乎是正确的:

Hibernate: select from serverlist.configtool_group this_ inner join serverlist.configtool_app_site a1_ on this_.app_site_group_id=a1_.app_site_group_id where this_.app_site_group_id=? order by a1_.name_eng asc

我错了... appSites 属性是在另一个 SQL 中设置的,它没有任何排序:

Hibernate: select ... from serverlist.configtool_app_site appsites0_ where appsites0_.app_site_group_id=?

4

1 回答 1

0

appSite.nameEng没有正确排序

你的类型appSiteHashSet. 所以从本质上讲,它没有秩序。

ClassCastException 的原因:

在您的查询中:

from Group as g inner join g.appSites as s where g.appSiteGroupId=10 order by s.nameEng asc

您正在连接(内部)两个实体(GroupAppSite),因此查询的结果肯定不是Group实体类型。您应该使用类的list()方法Query来处理这种查询。

于 2013-07-03T18:12:36.680 回答