简体   繁体   中英

Accessing a Map in Grails Groovy domain objects

I'm getting some very odd results with Grails whereby I create and save an object to the db, can see that created via dbconsole, but then cannot retrieve it using dynamic finders.

My application reads messages from a queue sequentially, receiving an activation message then a series of movement messages. All messages carry a common train_id field. I process the activation, save it, then later when a movement message comes extract the train_id from the movement message and use that to find the persisted object.

Here's the code

// from the activation
def train = new Train ()
...set attribute values here
train.save(flush: true, failOnError: true)

// then for the movement
def handleTmMessage(Map tm) {
Map body = tm["body"]
System.out.println "Movement: ${body}"
System.out.println "body[]:" + body["train_id"] + ":"
System.out.println "body.getAt:" + body.getAt("train_id") + ":"
System.out.println "Looking for train: " + body["train_id"]
String lookupId = body["train_id"]                         // <- something is wrong here
def train = Train.findByTrainUid(lookupId)               // <- This does not work
//def train = Train.findWhere(trainUid: "042H41MW14")      // <- This works !
//def train = Train.findWhere(trainUid: body["train_id"])  // <- This does not work
println train
println train.trainUid
}

Here's the output Movement:

[actual_timestamp:1421261040000, auto_expected:true, correction_ind:false, current_train_id:, delay_monitoring_point:false, direction_ind:DOWN, division_code:60, event_source:AUTOMATIC, event_type:DEPARTURE, gbtt_timestamp:1421260980000, line_ind:, loc_stanox:04025, next_report_run_time:4, next_report_stanox:04010, offroute_ind:false, original_loc_stanox:, original_loc_timestamp:, planned_event_type:DEPARTURE, planned_timestamp:1421261010000, platform:, reporting_stanox:00000, route:2, timetable_variation:1, toc_id:60, train_file_address:null, train_id:042H41MW14, train_service_code:13560015, train_terminated:false, variation_status:LATE]
    body[]:042H41MW14:
    body.getAt:042H41MW14:
    Looking for train: 042H41MW14
    null
    java.lang.NullPointerException: Cannot get property 'trainUid' on null object

NOTE: the messages use train_id and the Train object uses trainUid

So I think the lookup to the Map is somehow failing? Any ideas greatly appreciated

Martin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM