Search code examples
hibernategrailsgroovygrails-orm

Grails Map<String,Object> causes exception Integer cannot be cast to String


So, the problem I have is very strange per grails behaviour.

domain:-

public class Product {
    Group group  
    Map<String, Object> configuration 
...
...
}

below is the configuration passed while creating a new object.

configuration:[ emailAddress:null, **users:41**...]

users above is Integer but when I try saving the object as below.

instance.save flush: true

It says :-

Error java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
| Error     at org.hibernate.type.descriptor.java.StringTypeDescriptor.unwrap(StringTypeDescriptor.java:40)
| Error     at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$1.doBind(VarcharTypeDescriptor.java:52)
| Error     at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:91)

One solution is to convert integer to string and then save it.It works as I tried this.

Is there a better solution?


Solution

  • It is stated quite clearly in the Grails manual for you case: In this case the key and value of the map MUST be strings.

    See that section ('Maps of Objects') for an alternate option (using a hasMany on your Map to define the value type).