【问题标题】:Hibernate - Add orderBy() to DetachedCriteria if none is present?Hibernate - 如果不存在,将 orderBy() 添加到 DetachedCriteria?
【发布时间】:2019-07-20 08:54:13
【问题描述】:

我正在尝试从自定义查询对象动态构建查询。

在代码中的某一时刻,orderBy 可能会添加到 DetachedCriteria。稍后,我想添加一个 orderBy,如果 DetachedCriteria 中不存在任何一个。浏览文档,我似乎找不到任何方法来访问此信息。

有没有办法做到这一点?

(当然如果不可能的话,我会围绕这个重构我的代码)

【问题讨论】:

    标签: java hibernate detachedcriteria


    【解决方案1】:

    你可以试试这个:

        DetachedCriteria detached; //intialized DetachedCriteria
        Session s; //intialized hibernate session
    
        //get the criteriaImpl executing the query 
        CriteriaImpl executableCriteria = (CriteriaImpl) detached.getExecutableCriteria(s);
    
        //return new iterator of the OrderEntries
        Iterator<CriteriaImpl.OrderEntry> orderEntryIterator = executableCriteria.iterateOrderings();
    
        //check if it has an order entry
        if (orderEntryIterator.hasNext()) {
    
        }
    

    希望它会有所帮助。

    【讨论】:

    • 嗯...不幸的是,我在构建查询时无法访问会话,因此不确定这是否适用于我的用例。好建议!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    相关资源
    最近更新 更多