GrailsInAction / graina2

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

Error in Section 5.2.2 When creating new Admin User #114

Open nassaleh opened 9 years ago

nassaleh commented 9 years ago

Im currently using Grails v2.4.4 and Grails in Action (2nd edition). In section 5.2.2, when trying to run the following script from the console I get the following error:

Script: import com.grailsinaction.* grails.util.Environment.executeForCurrentEnvironment(new BootStrap().init)

Output Exception thrown org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : com.grailsinaction.User.dateCreated; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.grailsinaction.User.dateCreated

It seems the cause of the problem is also when creating an Admin new User(loginId: "admin", password: "secret", profile: profile).save(failOnError: true)

Any ideas on how to fix this?

Asw20 commented 9 years ago

As a workaround you can add "=new Date()" after field dateCreated in the Post.groovy and User.groovy file "Date dateCreated" -­> "Date dateCreated = new Date()" to default the current date or change the boostrap script to add this information.

If your question is why it is not an issue when you execute the application with "run-app" and it is when you execute it from the consol... I have no clue ;-)

pledbrook commented 9 years ago

So sorry for leaving this report for so long. I tried it out with the chapter 14 source, but didn't run into this specific issue.

The main reason you might see this is that auto timestamping has been disabled on the class. Search your source code for any references to autoTimestamp. Otherwise I'm at a bit of a loss. Auto timestamping is on by default and seems to work fine.

One other thing to check is whether you have added a nullable: false constraint to the dateCreated field. That may cause the issue, although I would expect a validation error rather than a data integrity one.

dimascrocco commented 9 years ago

Ouch, this problem is still happening. If you try to summon a Panda like this:

def p = new Panda(name: "Pandinha", dateCreated: new Date()) println p.dateCreated

The output is null

Otherwise, if you work for WWF and do this: def p = new Panda(name: "Pandinha") p.createdDate = new Date()

The output is not null and the baby panda is saved! OMG, plz, save the Pandas!

pledbrook commented 9 years ago

@dimascrocco That looks like a different issue and is related to Grails' special handling of domain class constructors. It seems that the constructor ignores the autotimestamp fields (dateCreated and lastUpdated) during data binding. However, if you set the relevant property directly as in your second example, no special handling takes place and the property value changes.

It's important when using Grails to understand when you're using the data binding feature, because the behaviour is quite distinct from normal Groovy.

dimascrocco commented 9 years ago

@pledbrook, after READING the release notes for grails 2.4.4, I found a workaround there that solves the problem. All Pandas are in good shape now. Thank you.

pledbrook commented 9 years ago

@Asw20 Following up on @dimascrocco's research, your problem seems to be connected with a particular version of the Hibernate 4 plugin, as described in JIRA.