Hibernate - Add orderBy() to DetachedCriteria if none is present?

158 Views Asked by At

I'm trying to build a query on the fly from a custom query object.

At one point in the code, the orderBy may be added to a DetachedCriteria. Later down the line, I'd like to add an orderBy should none already exist in the DetachedCriteria. Looking through the docs, I can't seem to find any way to access this information.

Is there some way to do this?

(Of course if it's impossible, I'll just refactor my code around this)

1

There are 1 best solutions below

1
Andrianekena Moise On

Can you try this :

    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()) {

    }

Hope it will help.