strange lazy loading failure

29 views
Skip to first unread message

Mike Wood

unread,
Oct 29, 2012, 7:33:30 PM10/29/12
to grails-jax...@googlegroups.com
I am using some scaffolded services to load some existing domain classes. For some reason when the classes get serialized (JSON) the child objects appear empty. I can inspect them before the resource returns them to be serialized and get values (via log statements). It's very strange. 

My parent class (Job) has two child classes - User and JobStatus. Neither are loaded correctly. In the code below the three log.error statements produce values. The final JSON shows nulls (it does serialize as the object with fields - just all are null). If I replace the jobStatus object as in the commented out line it will serialize values. I've also tried setting the hibernate mapping in Job.groovy to lazy: false. Seems like the job.jobStatus proxy is loaded according to everything except the serialization.

@Consumes(['application/xml','application/json'])
@Produces(['application/xml','application/json'])
class JobResource {
    
    def jobResourceService
    def id
    
    @GET
    Response read() {
        def job = jobResourceService.read(id)
        log.error job.user.username
        log.error job.jobStatus
        // job.jobStatus = JobStatus.get(job.jobStatus.id)
        log.error job.jobStatus
        ok job
    }

Mike Wood

unread,
Oct 30, 2012, 11:03:21 PM10/30/12
to grails-jax...@googlegroups.com
I need to correct myself. If I add lazy: false in the mapping it does work correctly (i.e. the child object will serialize correctly). Shame to have to force this at the ORM level though.

@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.NONE,
                getterVisibility=JsonAutoDetect.Visibility.NONE,
                isGetterVisibility=JsonAutoDetect.Visibility.NONE)
class Job implements Serializable
{
    static belongsTo = [user: User]

    @JsonProperty('id')
    Integer id
    .....
    @JsonProperty('user')
    User user;

    static mapping = {
        ....
        user(lazy: false)
    }
}
Reply all
Reply to author
Forward
0 new messages