1

The following test works in a Grails controller:

Domain class:

package freight

class CodeList {
    private static final long serialVersionUID = 1

    String codeType
    String code
    String description
  
    static constraints = {
        codeType nullable: false, blank: false
        code nullable: false, blank: false, unique: 'codeType'
        description nullable: true, blank: true
    }

    static mapping = {
    }
}  

Code in controller action:

def o = CodeList.get(10254)
println o as JSON  

Output:

{"id":10254,"code":"AFR","codeType":"AOC_CODE","description":"Africa"}  

When I try this in grails console I get this error:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'freight.CodeList : 10254' with class 'freight.CodeList' to class 'freight.CodeList'
    at freight.CodeList.get(CodeList.groovy)
    at freight.CodeList$get.call(Unknown Source)
    at ConsoleScript1.run(ConsoleScript1:3)

enter image description here

What's wrong with this?

The following runs without error and gets the same row:

def row = CodeList.findById(10254)
def rows = CodeList.findAllWhere(id: 10254L) // println rows[0]

Alternative solution

I installed the Grails web-based Groovy console plugin and that works much better and faster than grails console. Wish I have found this sooner.

enter image description here

A.W.
  • 2,858
  • 10
  • 57
  • 90
  • Have you seen this? https://community.bonitasoft.com/questions-and-answers/i-get-cannot-cast-object-object-groovy-exception – see sharper Aug 26 '21 at 01:00
  • @seesharper I read the thread, but don't think it's related to Grails. The following does work in Grails GroovyConsole without error: `def rows = CodeList.findAllWhere(id: 10254L) println rows[0]` which is the same row. `CodeList.findById(10254)` also works fine – A.W. Aug 27 '21 at 10:09
  • Could you send a link to your PoC project repo? – Michal_Szulc Aug 27 '21 at 13:39

0 Answers0