0

I have (among others) two domain classes:

class Course {
  String name
  ...
}

class Round {
  Course course
  String startweek // e.g. '201504'
  String endweek   // e.g. '201534'
  String applcode  // e.g. 'DA542133'
  ...
}

Application codes may be issued at several occasions and are then concatenated with 'applcode's separated by blanks. As I am streaming and parsing large amount of data (in XML format) from different sources, I might stumble on the same data from several sources, so I look up the records in the database to see if I may discard the rest of the stream or not. This is possible as the outermost tag contains data stating the above declared attributes. I search the database using:

def c = Course.findByName(name);
def found =
 Round.findByCourseAndStartweekAndEndweekAndApplcodeLike(c, sw, ew,'%'+appc+'%')

where the parameters are fairly obvious and which works well but I find these 'findByBlaAndBlablaAnd...' very long and not very readable. My aim here is to find some more readable and thereby more comprehensible method. I have started to read about Criteria and HQL but I think one example or two would help me on the way.


Edit after reading the pages on the link provided by @injecteer:

It was fairly simple to make out the query above. I have worse thing to figure out but the query in my example became with criteria:

def found = Round.createCriteria().get {
  eq   ('course', c)
  eq   ('startweek', sw)
  eq   ('endweek', ew)
  like ('applcode',  '%'+appc+'%')
};

Much easier to read and understand than the original question.

Serafim
  • 484
  • 3
  • 10
  • Save yourself the trouble and start making heavy use of named queries: http://grails.github.io/grails-doc/latest/ref/Domain%20Classes/namedQueries.html – Joshua Moore Jul 13 '15 at 20:29
  • 1
    where did you look for examples on hql and criteria? there are tons of examples on https://grails.github.io/grails-doc/latest/guide/GORM.html#querying – injecteer Jul 13 '15 at 21:01
  • Thank you @injecteer, as stated above I just started reading and with your link it was quite simple. – Serafim Jul 13 '15 at 22:06

0 Answers0