Search code examples
grailsgrails-orm

GORM: saving a manyTomany relationship


just trying to persist a relationship between GORM entities, and an overflow error appends on mapping during save.

1) I create a manyTomany relationship between User and DataStore:

User entity: 
...
static belongsTo = DataStore
    static hasMany = [groups: Groups,dataStore:DataStore]

    Profile profile
    Contacts contact
    DataStore dataStore
...

DataStore entity:
...
static belongsTo = [service:Service]
    static hasMany = [users:User]

    Service service
    List<User>   users
...

2) Calling the service from a controller to save datas:

 UserRole.create user, roleCustomer, true
        UserRole.create user, roleAdmin, true
        dataStoreService.createDS('ds',profile.service,user)

3) Service logic:

 @Transactional
    def createDS(ds,service,user) {
        def key = service.domainkey
        if (user && key) {
            DataStore ds = new DataStore(ds:ds)
            ds.validate() ? ds.save(flus:true) : ds.errors.allErrors.println()
            ds.addToUsers(user).save(flush:true)
            service.addToDataStore(ds).save(flush:true)
            user.setDataStore(ds)
...}

4) The weird error i need to solve:

Stacktrace follows: org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException: Executing action [sendOrder] of controller [$$.StoreController] caused exception: Runtime error executing action at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195) at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63) at net.bull.javamelody.JspWrapper.invoke(JspWrapper.java:149) at net.bull.javamelody.JdbcWrapper$DelegatingInvocationHandler.invoke(JdbcWrapper.java:259) at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:202) at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:175) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) at java.lang.Thread.run(Thread.java:680) Caused by: org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException: Runtime error executing action ... 9 more Caused by: java.lang.reflect.InvocationTargetException ... 9 more Caused by: java.lang.StackOverflowError at org.apache.commons.validator.EmailValidator.stripComments(EmailValidator.java:246) at org.apache.commons.validator.EmailValidator.isValid(EmailValidator.java:95)

Any help please?


Solution

  • just redone my model following hibernate doc:

    class User implements Serializable {
    
        static hasMany = [dataStores:DataStore,contacts:Contacts,groups: Groups]
    
        Profile profile
        Contacts contacts
        List<DataStore> dataStores
    

    with contraint: dataStores nullable:true

    And DS entity:

    class DataStore implements Serializable{
    
        static hasMany = [users:User,contacts:Contacts]
        static belongsTo = [service:Service]
    
        Service service
        User users
    

    removes definetely the several overflows and keeps the model reliable.