English 中文(简体)
Sorting Objects Based on Custom Domain Class Methods
原标题:

I have a domain class, in which I ve defined some methods which give the object a score based on different algorithms (eg. popularity).

I now want to retrieve a list of these objects sorted by one of these scores (eg. descending by popularity score).

Is there a way to do with with GORM?

Example class:

class SomeObject {
    String title

    Integer popularity() {
        //some algorithm
        return popularity
    }
}
最佳回答

Jared and you are right. Since it s a calculated value and not stored in the DB, GORM has no way of knowing anything about it. If you do as you say and create a new column for the value and then update it as needed you can use the SomeObject.list(order: desc , sort: popularity , max:10) syntax.

问题回答

There are three solutions I can think of. Assuming your dataset is small you can just return the entire list then sort it. For example

objectList.sort{popularity()}

Note based off the following link. http://jlorenzen.blogspot.com/2008/05/groovy-sort-list.html

If the calculation formula is simple you can always use a HQL query instead of the default find by methods. HQL is a query language similar to SQL that works on Hibernate objects which is what Gorm is built on. For more info see the following link. http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html If you don t need the calculated field to be updated in real-time and your calculations are to complex for HQL you can schedule a job to update the calculation results every hour or so, then store the generated result in a field of your domain class. For more information on Grails job scheduling see the following link. http://grails.org/Job+Scheduling+%28Quartz%29





相关问题
grails + gwt request handling via controllers

I am new to gwt. I am trying to integrate gwt+grails.Can anybody provide me a good example for handling the request using grails controllers and not a custom servlet.I will be really thankful if ...

Error loading the grails gwt module xml

I ve installed the plugin from this article by Peter http://www.cacoethes.co.uk/blog/groovyandgrails/the-command-pattern-w.... While compile time its not able to find the module file which is present ...

Sorting Objects Based on Custom Domain Class Methods

I have a domain class, in which I ve defined some methods which give the object a score based on different algorithms (eg. popularity). I now want to retrieve a list of these objects sorted by one of ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

热门标签