使用ORDER BY子句时Java Hibernate查询卡住了

时间:2012-08-09 10:47:22

标签: java hibernate hang clause

我有以下HQL查询:

SELECT bu FROM Backup bu WHERE bu.status = :status ORDER BY bu.container.techId ASC

每当出现查询结果时> 0查询卡住了,这意味着query.getResultList()永远不会返回。 但是,如果我删除了ORDER BY子句,只留下SELECT bu FROM Backup bu WHERE bu.status =:status它运行良好

我的课程是:

public class Backup implements Serializable {

  @Id
    @Column(name="ID")
    @GenericGenerator(name = "generator", strategy = "guid", parameters = {})
    @GeneratedValue(generator = "generator")
    private String id;

    @Column(name="PVAID", nullable=true)
    private String PVAID;

    @Column(name="Error", nullable=true)
    private String error;

    @Column(name="\"Size\"", nullable=false)
    private Long backupSize;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="startdate", nullable=false)
    private Date startDate;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="enddate", nullable=true)
    private Date endDate;

    @Column(name="status", nullable=false)
    private Short status;

    @ManyToOne
    @JoinColumn(name="fullbackupid", referencedColumnName="ID", nullable=true)
    private Backup fullBackup;

    @ManyToOne
    @JoinColumn(name = "ContainerID", referencedColumnName="ID", nullable=false)
    private Container container;

}

public class Container implements Serializable {

   @Id
    @Column(name="ID")
    private String id;

    @ManyToOne
    @JoinColumn(name="HardwareNodeID", referencedColumnName="ID", nullable=false)
    private HardwareNode hardwareNode;

    @ManyToOne
    @JoinColumn(name="BackupServerID", referencedColumnName="ID", nullable=false)
    private BackupServer backupServer;

    @Column(name="TechID", nullable=false)
    private Integer techId;

    @Column(name="Exclude", nullable=false)
    private Boolean exclude;

    @Column(name="retry_backup", nullable=true)
    private Short retryBackUp;

    @Column(name="retry_delay", nullable=true)
    private Short retryDelay;

}

执行查询的代码

 public List<T> getAllByFilter(final boolean isNamedQuery, final String queryString, final HashMap<String, Object> fields, final int startRecord, final int maxResults) {
    logger.log(Level.INFO, "Loading all paginated");
    final EntityManager em = emf.createEntityManager();
    Query query = null;
    if(isNamedQuery) {
        query = em.createNamedQuery(queryString);
    } else {
        query = em.createQuery(queryString);
    }
    final Set<?> set = fields.entrySet();
    final Iterator<?> i = set.iterator();

    while(i.hasNext()){
      final Map.Entry me = (Map.Entry)i.next();
      final String field = (String)me.getKey();
      final Object value = me.getValue();
      System.out.println(me.getKey() + " : " + me.getValue() );
      query.setParameter(field, value);
    }
    query.setFirstResult(startRecord);
    if (maxResults!=0) query.setMaxResults(maxResults);
    return query.getResultList();
}

SQL查询似乎也很好:

13:45:03,583 INFO  [STDOUT] Hibernate: select top 41 backup0_.ID as ID153_, backup0_.IsFullBackup as IsFullBa2_153_, backup0_.PVAID as PVAID153_, backup0_.Error as Error153_, backup0_.[Size] as Size5_153_, backup0_.startdate as startdate153_, backup0_.enddate as enddate153_, backup0_.status as status153_, backup0_.ManualBackup as ManualBa9_153_, backup0_.fullbackupid as fullbac10_153_, backup0_.ContainerID as Contain11_153_ from Backups backup0_, Containers container1_ where backup0_.ContainerID=container1_.ID and backup0_.status=? order by container1_.TechID DESC

底层数据库是SQL Server。

哪里可能是问题?有人遇到类似的事吗? 感谢

0 个答案:

没有答案