Announcement Announcement Module
Collapse
No announcement yet.
Grails Noob Frustration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Grails Noob Frustration

    Hello,
    After much frustration, I've decided to ask for help here. I am pretty new to grails and what seems to be a fairly straight-forward procedure is giving an error message. I have one view that is trying to create a sales order object and several sales order material objects when the save button is clicked. Right now I'm simply trying to save one sales order material to be tied to the sales order. Any assistance is appreciated. Let me know if you need anything else to diagnose this problem! Thank you!!

    Here is the exception:
    org.hibernate.AssertionFailure: null id in objecteo.ms.SalesOrderMaterial entry (don't flush the Session after an exception occurs)

    at java.util.concurrent.ThreadPoolExecutor.runWorker( ThreadPoolExecutor.java:1110)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:603)

    at java.lang.Thread.run(Thread.java:679)

    And here is the code:
    Code:
    def save = {
            def salesOrderMaterial = new SalesOrderMaterial(params["salesOrderMaterial[0]"])
            
            def salesOrderInstance = new SalesOrder(params.salesOrder).addToSalesOrderMaterials(salesOrderMaterial)
            try{
            if (salesOrderInstance.save(flush: true)) { 
                
                flash.message = "${message(code: 'default.created.message', args: [message(code: 'salesOrder.label', default: 'SalesOrder'), salesOrderInstance.id])}"
                redirect(action: "show", id: salesOrderInstance.id)
            }else {
                render(view: "create", model: [salesOrderInstance: salesOrderInstance])
            }
            }catch(Exception e){
                System.out.println(e.message)
            }
        }

  • #2
    I also want to note that the code runs fine without the .addToSalesOrderMaterials bit.
    Thanks

    Comment


    • #3
      Problem solved. Turns out the SalesOrderMaterial wasn't validating properly.

      Comment


      • #4
        This is a typical thing you need to get used to with GORM. You tend to want to use the GORM constraints to perform validation but the save() operation fails somewhat silently if validation fails. You can override this by setting the following in your conf/config.groovy file.
        Code:
        grails.gorm.failOnError=true
        This is good for development but probably not a good idea for production.

        Comment


        • #5
          My preference is to check for validation errors with "if (!foo.save()) { ... }" or "if (!foo.validate()) { ... }", and occasionally use "foo.save(failOnError: true)" when I know that the save should succeed (e.g. in BootStrap where I'm setting the values). But in general you should expect users to submit invalid data and throwing an exception for every validation error is overkill, plus it pollutes your code with try/catch blocks.

          Comment

          Working...
          X