GrailsInAction / graina2

Source code for the 2nd edition of Grails in Action
90 stars 92 forks source link

Chapter 5.1.1 - Error when creating admin user in BootStrap.groovy #15

Open steven-s opened 11 years ago

steven-s commented 11 years ago

When launching the app with the bootstrap file from github, I get an exception during the create admin portion.

Fresh Database. Creating ADMIN user.
| Error 2013-06-06 10:11:52,448 [localhost-startStop-1] ERROR hibernate.AssertionFailure  - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
Message: null id in com.grailsinaction.Profile entry (don't flush the Session after an exception occurs)

This is the code I have copied from the git repo:

private createAdminUserIfRequired() {
    if (!User.findByLoginId("admin")) {
        println "Fresh Database. Creating ADMIN user."

        def profile = new Profile(email: "admin@yourhost.com")
        new User(loginId: "admin", password: "secret", profile: profile).save()
    }
    else {
        println "Existing admin user, skipping creation"
    }
}
pledbrook commented 11 years ago

Thanks for the report. This is because Profile doesn't 'belong to' the User class, so the associated profile isn't automatically saved when we save the user. I'm pretty sure we plan to add

static belongsTo = [user: User]

to the Profile domain class, which will fix the problem.