问题
I am inserting some data into the database but some objects aren't inserting even though I can't see any validation errors. What is the best way to error with stacktrace or with sql so i can figure out what is wrong?
for example I do , new XXXXX(property: "blah").save(flush:true)
I don't see errors on startup (grails run-app) but i also don't see my data. I do see the data for many of my objects so i'm sure it's something about my objects, validation or even associations but I need a simple way to see the issue in the log/console...
i'm sure this is easy, but tia...
回答1:
I recommend letting save() throw an exception if it fails during bootstrap, since a validation error will be caused by your code, not user input.
new Xxxxx(property: "blah").save(failOnError: true)
The exception will contain a pretty wordy block of text, but check the first line, which will look like:
grails.validation.ValidationException: Validation Error(s) occurred during save():
Field error in object 'mypackage.Xxxxx' on field 'myProperty': rejected value [null]
回答2:
First and formost, I'd write a unit test around your domain. If that seems to work, but you're still having issues in Bootstrap, for each save, I then check the domain for errors..
def xxxxx = new XXXXX(...)
xxxxx.save(flush:true)
def errors = xxxxx.errors
I'll then put a break point in there so I can inspect the errors object. Otherwise, you can println the errors object, and usually glean enough from that to figure out what is going on.
来源:https://stackoverflow.com/questions/5355329/what-is-the-best-way-to-debug-bootstrap-groovy