Search code examples
mongodbgrailsinterfacepersistencegrails-orm

Persisting Classes implementing an interface with GORM MongoDB Plugin


I am having a problem while persisting a class. I have a class called Scraper which uses an interface called Paginator. There are several implementations of the Paginator interface which will be instantiated at runtime. So the structure looks like this:

class Scraper {

    //some code

    Paginator paginator

    //more code

    def Scraper(Paginator paginator){
        this.paginator = paginator
    }
}

and then there are the concrete implementations of the paginator interface lets say paginatorA and paginatorB. So now I am trying to do the following:

PaginatorA p = new PaginatorA()
Scraper s = new Scaper(p)

s.save(flush:true)

...and what it get is:

Error Error executing script TestApp:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name     'mongoDatastore': Cannot resolve reference to bean 'mongoMappingContext' while setting bean property 'mappingContext';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoMappingContext': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException (Use --stacktrace to see the full trace)

Can anybody tell me what to make of this? I guess it has something to do with the Mapper because it doesn't know which concrete Paginator to use or how to persist it? If that is the case then how can I tell the framework what to do? I tried to come up with a solution for hours now and am really frustrated so any help would be really really appreciated.

Oh btw I also tried implementing against the concrete implementation (PaginatorA) ... this works perfectly fine since my assumption that it has something to do with the paginator interface.

Thanks for any response...


Solution

  • The error is bad, you should probably raise a JIRA issue for that, but fundamentally there are 2 problems I can see with the code:

    1. Your persistent classes must have a public no-args constructor as per any JavaBean, by adding a constructor that takes your interface, you are no longer providing one

    2. Your Scraper class needs to mark the 'Paginator' as transient to tell the persistence engine not to attempt to persist the 'paginator' property. Since this a custom interface it will not know how to persist it.