Open nassaleh opened 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 ;-)
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.
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!
@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.
@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.
@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.
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?