1

我有两个实体问题和Issue_Tracker。我在一对多映射中加入了这两个表。在Issue_Tracker中,我们可以有多个条目,例如

  issue_id  tracker_status      tracked_time
    123     Assigned           1/8/2013 11:44
    123     Assigned           1/8/2013 11:45
    123     Completed          1/8/2013 11:52
    32      Assigned           1/9/2013 16:46
    33      Assigned           1/9/2013 16:47
    33      Cancel             1/9/2013 16:49 

我想通过 tracked_time 订购 Issue_tracker 实体,因此我只能获得最后一次更新tracker_status。我正在使用标准来获取数据,但是我得到了第一个条目,例如:对于 issue_id 123,我将 tracker_status 设置为 Assigned ,但我想tracker_status : Completed获取id 123. 如何做到这一点。我的DAO如下

     Criteria criteria = session.createCriteria(Issue.class);
     criteria.setFirstResult(from);
     criteria.setMaxResults(size);
     criteria.setFetchMode("Issue.issueTracker",FetchMode.JOIN);
     criteria.add(Restrictions.eq("status", "Escalate To"));
      return criteria.list();

问题.java

public class Issue implements Serializable
        {
        private Integer issue_id;
        private String  issue_description;
        private Date issue_raised_date;
        private Set<Issue_Tracker> issueTracker = new HashSet<Issue_Tracker>(0);



        @OneToMany(fetch=FetchType.LAZY, mappedBy="issue_id")
        public Set<Issue_Tracker> getIssueTracker() {
        return issueTracker;
    }

        public void setIssueTracker(Set<Issue_Tracker> issueTracker) {
        this.issueTracker = issueTracker;

Issue_Tracker.java

public class Issue_Tracker implements Serializable
{  
     private Integer   issue_id;
     private String    tracker_status;
     private Timestamp tracked_time;

我希望你们能得到我想问的问题,有什么帮助吗?

4

1 回答 1

2

向映射的集合添加 @OrderBy 注释。这应该返回按跟踪时间排序的集合中的实体。集合中的第一个实体应该是最新的。

//Issue.java
    @OneToMany(fetch=FetchType.LAZY, mappedBy="issue_id")
    @OrderBy("tracked_time DESC")
    public Set<Issue_Tracker> getIssueTracker() {
    return issueTracker;

您可能还想尝试:

 Criteria criteria = session.createCriteria(Issue.class);
 criteria.setFirstResult(from);
 criteria.setMaxResults(size);
 criteria.setFetchMode("Issue.issueTracker",FetchMode.JOIN);
 criteria.add(Restrictions.eq("status", "Escalate To"));
 Criteria subCriteria = criteria.createCriteria("issueTracker");
 subCriteria.addOrder(Order.desc("tracked_time"));
 return criteria.list();
于 2013-01-10T10:25:35.883 回答