0

I am trying to create some user for login testing. So that, I have created some user on bootstrap.

User user = new User(username: 'user', userPassword: 'user', userEmail: '[email protected]').save()

Then I try to login. The result was unsuccessful. Even database got no record on the user I have create on BootStrap.groovy

Will different package on bootstrap with my domains that affects?

Domain Package

package com.abc.myapp

BootStrap Package

package myapp

or is there any other possibility on this "bootstrap creating user error"

2
  • Ensure the insert didn't fail by adding the following to save, save(failOnError: true), also, you're going to end up with a lot of duplicate users with this in place unless you have relevant database constraints in place or you're rebuilding the db on boot.
    – Mike W
    Feb 5, 2018 at 11:34
  • Thanks for that !! Finally I can know whats going on. The code is very useful
    – Trainee
    Feb 6, 2018 at 1:13

1 Answer 1

0

Bootstrap is not related to package.This file can be used for any custom logic you want to happen during application startup.Excellent use of the file is to preload some data in our database.

 class BootStrap { 
            def init = { servletContext ->
            new User(username:'user',userPassword:'user',userEmail:'[email protected]').save(failOnError: true)
                      }
}

save(failOnError: true) will give you the cause if it fails to save data.To avoid duplicate data creation use constrain in you domain class .

static constraints = {
        username unique: true, blank: false
        } 
1
  • Thanks for the explanation and reminder. The code is useful.
    – Trainee
    Feb 6, 2018 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.