PrinceAdeleke opened a new issue, #14398:
URL: https://github.com/apache/grails-core/issues/14398

   I've created a small grails application where a Book has an Author. I create 
an instance of book and author and saved the objects to the database. In the 
Book class I have an event `beforeValidate` that tags the book by concatenating 
the title and author name. When saving the object an exception is thrown 
`grails.validation.ValidationException: Validation error occurred during call 
to save()`. 
   
   ### Version:
   #### Grails
   4.0.0.RC2
   #### Mongodb
   7.0.0 (`org.grails.plugins:mongodb:7.0.0` in build.gradle)
   
   ### Expected Behaviour
   The book should update without any constraint violations.
   
   ### Actual Behaviour
   When saving the book an error is thrown. The error states that the author 
name has been rejected and must be unique.
   
   `arguments [name,class library.Author,Charles Perrault]; default message 
[Property [{0}] of class [{1}] with value [{2}] must be unique]`. See 
stacktrace below.
   
   ## Stacktrace:
   ```
   - Field error in object 'library.Book' on field 'author.name': rejected 
value [Charles Perrault]; codes 
[library.Author.name.unique.error.library.Book.author.name,library.Author.name.unique.error.author.name,library.Author.name.unique.error.name,library.Author.name.unique.error.java.lang.String,library.Author.name.unique.error,author.name.unique.error.library.Book.author.name,author.name.unique.error.author.name,author.name.unique.error.name,author.name.unique.error.java.lang.String,author.name.unique.error,library.Author.name.unique.library.Book.author.name,library.Author.name.unique.author.name,library.Author.name.unique.name,library.Author.name.unique.java.lang.String,library.Author.name.unique,author.name.unique.library.Book.author.name,author.name.unique.author.name,author.name.unique.name,author.name.unique.java.lang.String,author.name.unique,unique.library.Book.author.name,unique.author.name,unique.name,unique.java.lang.String,unique];
 arguments [name,class library.Author,C
 harles Perrault]; default message [Property [{0}] of class [{1}] with value 
[{2}] must be unique]
   ```
   
   ### Example Application
   
   #### Bootstrap.groovy
   
   ```
   package bookstore
   
   import library.Author
   import library.Book
   
   class BootStrap {
   
       def init = { servletContext ->
           Author.findAll().each { it.delete(flush: true, failOnError: true) }
           Book.findAll().each { it.delete(flush: true, failOnError: true) }
   
           Author author = new Author(name: "Charles Perrault").save(flush: 
true, failOnError: true)
           new Book(title: 'Little Red Riding Hood', author: 
author).save(flush: true, failOnError: true)
   
           Book.withNewSession {
               Book book = Book.findByTitle('Little Red Riding Hood')
               book.save(failOnError: true)
           }
       }
   }
   ```
   
   #### Author.groovy
   
   ```
   package library
   
   import org.bson.types.ObjectId
   
   class Author {
   
       ObjectId id
       String name
   
       static constraints = {
           name(blank: false, unique: true)
       }
   }
   ```
   
   #### Book.groovy
   ```
   package library
   
   import org.bson.types.ObjectId
   
   class Book {
   
       ObjectId id
       Author author
       String title
       String tag
   
       static mapping = {
           version false
       }
   
       static constraints = {
           title(blank: false)
           author(nullable: false)
       }
   
       def beforeValidate() {
           tag = ("${title} ${author.name}").toLowerCase().split(' ').join('-')
       }
   }
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to