Search code examples
grailsgrails-orm

Grails: Try to save a object with nested objects. No error but no ID


This is my application: I have the user that is compsed by three elemtents

  1. User (Username, Password, Role, etc.. From SpringSecurity)
  2. AppUser (Base details like surname, etc)
  3. Detail User (Details specific by application. Working area, etc etc)

AppUser contains an instance of User and DetailUser (this lastone nullable)

This is my code:

 if (user.validate() &&  appUser.validate() ){
            user.save()
            appUser.user = user
            appUser.save()

            appUserDetail.appUser = appUser
            appUserDetail.save()

        }

No error validations, but the ID of my appUser is null!

Sincerely I don't understand why.

UPDATE

Hibernate.AssertionFailure an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: null id in it.geny.auth.AppUser entry (don't flush the Session after an exception occurs)

Solution

  • Finally I found a solution.

    Searching on SO about this issue I found many post about this. Some people said that the :

    <Domain>.get(id)
    

    maybe can close session, as consequence the object will not be saved. But where I call .get function?!?!?

    Here:

    def appUser = new AppUser(params)
    

    Inside I have a one-to-one rel to many domanins (ex. Region region). Inside the GrailsParameterMap I had params."appUser.region" with the ID of the Region.

    If I use the binder of grails (spring) (I cannot check the code, but if this true, I will find a .get(id) somewhere...), my object will not persisted.

    But , if I bind manually , ALL IS OK, my object is saved.