Custom Mapping with Mongo GORM Plugin

129 views
Skip to first unread message

Xristofer Obbit

unread,
Feb 6, 2011, 2:35:33 PM2/6/11
to Inconsequential
Is it possible to do custom property mapping? I am dumping twitter
JSON data directly into Mongo and I'd like to have a domain class that
wraps it as is. I'd rather use Java style variable naming conventions
and I need a way to get around the id conflict where the twitter data
contains the literal 'id' but 'id' in the plugin maps to '_id'. Below
is what I'd like to be able to do but I am not sure if it is
possible. Using the GMongo Java driver wrapper produces far cleaner
code then any work around I have come up with trying to use a domain
class. Any suggestions?

class Status {
Long statusId
String text
Date createdAt

static mapping = {
database 'twitter'
collection 'status'

createdAt column:'created_at'
statusId column:'id'
}
static constraints = { }
}

Graeme Rocher

unread,
Feb 9, 2011, 5:41:40 PM2/9/11
to inconse...@googlegroups.com
Hi Xristofer,

Currently you can't control the way properties are mapped to attributes, I'll see if I can get this into the next milestone of the Mongo plugin

Cheers

-- 
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware

Graeme Rocher

unread,
Feb 10, 2011, 4:42:30 AM2/10/11
to inconse...@googlegroups.com
Actually I found this is already implemented!

You need something like:


class Status {
Long statusId
String text
Date createdAt

static mapping = {
database 'twitter'
collection 'status'

createdAt attribute:'created_at'
statusId attribute:'_id'
}
static constraints = { }
}

In other words you use "attribute" not "column"

Cheers
 
-- 
Graeme Rocher
Grails Project Lead
SpringSource - A Division of VMware

Xristofer Obbit

unread,
Feb 11, 2011, 11:49:55 PM2/11/11
to inconse...@googlegroups.com
Thank you for the update.  I am noticing some issues with dynamic finders and having an attribute mapped to 'id'.  Using findByStatusId results in the document not being found.  So I attempted using findByText and I can see that it is trying to load '_id' into the statusId property.  Since I still insert into the database using the mongo driver, I can work around this by mapping id to something else, but I figured I'd pass it along.

class Status {
ObjectId id
Long statusId
String text
Date createdAt
static mapping = {
collection 'twitter.status'

createdAt attribute:'created_at'
statusId attribute:'id'
}
static constraints = { 
}
}

//////////////////////////////////////////////////

def s = new Status( createdAt: new Date(), statusId: 10L, text: "hi", ).save(flush:true)

def s = Status.findByStatusId(10L)
render s.text

2011-02-11 23:29:26,725 [http-8080-1] ERROR errors.GrailsExceptionResolver  - Exception occurred when processing request: [GET] /hashchan/status/load
Stacktrace follows:
java.lang.NullPointerException: Cannot get property 'text' on null object
at hashchan.twitter.StatusController$_closure3.doCall(StatusController.groovy:19)
at hashchan.twitter.StatusController$_closure3.doCall(StatusController.groovy)
at java.lang.Thread.run(Thread.java:636)

def s = Status.findByText("hi")

2011-02-11 23:29:28,724 [http-8080-1] ERROR errors.GrailsExceptionResolver  - Exception occurred when processing request: [GET] /hashchan/status/load
Stacktrace follows:
java.lang.IllegalArgumentException: Cannot convert value of type [org.bson.types.ObjectId] to required type [java.lang.Long] for property 'statusId': PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value
at org.grails.datastore.gorm.finders.FindByFinder.invokeQuery(FindByFinder.java:53)
at org.grails.datastore.gorm.finders.FindByFinder.doInvokeInternal(FindByFinder.java:46)
at org.grails.datastore.gorm.finders.DynamicFinder.invoke(DynamicFinder.java:72)
at org.grails.datastore.gorm.finders.DynamicFinder.invoke(DynamicFinder.java:183)
at org.grails.datastore.gorm.GormEnhancer$_enhance_closure2.doCall(GormEnhancer.groovy:171)
at hashchan.twitter.StatusController$_closure3.doCall(StatusController.groovy:19)
at hashchan.twitter.StatusController$_closure3.doCall(StatusController.groovy)
at java.lang.Thread.run(Thread.java:636)
Reply all
Reply to author
Forward
0 new messages