|
Hi,
I have a simple controller method that updates a domain class. The problem I am having is that despite having errors and not calling save(), I am getting bad data updated into my table. The problem is that the "title" field for this post in the "params" is blank, my setPostProperties() basically sets all the params to the fields on the "post". Then I call validate() and hasErrors(). I correctly get back the error message for the "title" field on my screen. However when I look at the record AFTER this method executes (note that the post.hasErrors() = true), the "title" field in the database has a single double quote in it ". Huh? Here is my controller code: def updatePost = { def post = Post.get( params.id ) if(post) { setPostProperties(post,params) post.modifiedAt = new Date() // validate it post.validate() } println "${post.hasErrors()} errors" if(!post.hasErrors() && post.save(flush:true)) { //.....render success back } else { def errMsgs = [] def locale = RequestContextUtils.getLocale(request) post.errors.allErrors.each { try { errMsgs.add(messageSource.getMessage(it,locale)) } catch(NoSuchMessageException e) { println "no such message" } } // DOJO specific !!! render(text:"<textarea>") render(builder:'json') { errors(errMsgs) } render(text:"</textarea>") } |
|
As Graeme said sometimes, hibernate is a little dumb and saves the data even if you don't call save (dirty saving).
But, if your title property has something in it (even a single quote) it will save. Try to send to us your setPostProperties method, so we can take a look... Cheers! On Fri, Jun 20, 2008 at 5:49 PM, interz <[hidden email]> wrote:
-- Fernando "Takai" http://flickr.com/photos/supeertakai http://fernandotakai.tumblr.com/ http://twitter.com/fernando_takai |
|
Here is the method that sets the properties. Note, the default grails "update()" generated controller is basically coded the same way and that works. (post.properties = params). If I call "discard" then this goes away, regardless, why should I have to call if save() is not executed?
private void setPostProperties(Post post, Map params) { def category = null def blog = null if (params.blogId != null) { blog = Blog.get(params.blogId) } if (blog == null) { blog = blogService.getDefaultBlog() } post.blog = blog post.author = AppUser.findByUsername("sysadmin") post.title = params.title.trim() post.setTags(params.tags) category = BlogCategory.get(params["category.id"]) post.setCategory(category) post.setCommentStatus(params.commentStatus) post.status = params.status if (params.textContent.trim().length() == 0) { post.errors.reject('leoBlog.postController.error.noPostContent') } else { post.setContentText(params.textContent) } }
|
|
actually, I was mistaken, there is no single double quote in the column. That was a postgres gui artifact. It is blank, and btw my constraints says blank is not allowed hence the error messages I get on the screen.
|
|
Well, i never went to grails sources, but maybe this behavior is expected.
You can take a look at this thread: http://www.nabble.com/Saving-is-automagical--tp15546732p15546732.html where says that dirty saving is a normal behavior and you should call obj.discard() if you don't want anything to be saved. On Fri, Jun 20, 2008 at 6:08 PM, interz <[hidden email]> wrote:
-- Fernando "Takai" http://flickr.com/photos/supeertakai http://fernandotakai.tumblr.com/ http://twitter.com/fernando_takai |
|
In reply to this post by interz
Any object that is on hibernate session scope will be updated if you alter properties. This is true even if you don't call save/update explicitly because hibernate flushes the session before close it (sending any object change to the database). So you need to "remove" that object from hibernate session scope using discard like Fernando points out.
Kind Regards On Fri, Jun 20, 2008 at 6:08 PM, interz <[hidden email]> wrote:
-- Marcos Silva Pereira http://marcospereira.wordpress.com "People who are crazy enough to think they can change the world, are the ones who do." |
|
Hibernate also flushes the session before performing certain queries. You can change this behaviour by setting the flush mode excplicitly, or if possible perform your queries before updated a session bound object.
2008/6/21 Marcos Silva Pereira <[hidden email]>: Any object that is on hibernate session scope will be updated if you alter properties. This is true even if you don't call save/update explicitly because hibernate flushes the session before close it (sending any object change to the database). So you need to "remove" that object from hibernate session scope using discard like Fernando points out. |
| Powered by Nabble | Edit this page |
