Grails
  1. Grails
  2. GRAILS-646

Reading objects with null values for primitive column types (i.e. int) results in exceptions even if nullable: true

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Won't Fix
    • Affects Version/s: 0.4
    • Fix Version/s: 1.3-RC1
    • Component/s: Persistence
    • Labels:
      None

      Description

      Declaring a property as an int, with nullable: true and listing it in optionals, you get the following error when trying to load an object that has a null value in that column:

      Grails Runtime Exception
      
      Error Details
      
      Message: Null value was assigned to a property of primitive type setter of Recipe.servesHowMany; nested exception is 
      org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of Recipe.servesHowMany 
      Caused by: 
      org.springframework.orm.hibernate3.HibernateSystemException: Null value was assigned to a property of primitive type setter of Recipe.servesHowMany; 
      nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of Recipe.servesHowMany 
      Class: RecipesController 
      At Line: [17] 
      Code Snippet:
      17: return fetchRecipeModel(Recipe.CATEGORY_DISH)
      18: }
      Stack Trace
      
      org.codehaus.groovy.runtime.InvokerInvocationException: org.springframework.orm.hibernate3.HibernateSystemException:
      Null value was assigned to a property of primitive type setter of Recipe.servesHowMany; nested exception is 
      org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of Recipe.servesHowMany
      at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:75) 
      at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713) 
      at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:559) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:449) 
      at groovy.lang.Closure.call(Closure.java:188) at groovy.lang.Closure.call(Closure.java:183) 
      at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsControllerHelper.handleAction(SimpleGrailsControllerHelper.java:369) 
      at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsControllerHelper.handleURI(SimpleGrailsControllerHelper.java:280) at 
      ....
      81 more Caused by: java.lang.IllegalArgumentException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
      at java.lang.reflect.Method.invoke(Method.java:585) 
      at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:42) ... 96 more
      

        Activity

        Hide
        Graeme Rocher added a comment -

        This one is not fixable, in fact it is not even isolated to Grails as you can reproduce this problem by using int values that have nullable columns with Hibernate. It will throw the same exception

        Show
        Graeme Rocher added a comment - This one is not fixable, in fact it is not even isolated to Grails as you can reproduce this problem by using int values that have nullable columns with Hibernate. It will throw the same exception
        Hide
        Marc Palmer added a comment -

        I think we can fix it, there is a setDefaultValue in hibernate which might do what we want. i.e. we extract default value for the column from domain class - possibly via a new constraint instead of property - and assign it to the default value during schema setup.

        http://www.hibernate.org/hib_docs/v3/api/org/hibernate/mapping/Column.html#setDefaultValue(java.lang.String)

        Show
        Marc Palmer added a comment - I think we can fix it, there is a setDefaultValue in hibernate which might do what we want. i.e. we extract default value for the column from domain class - possibly via a new constraint instead of property - and assign it to the default value during schema setup. http://www.hibernate.org/hib_docs/v3/api/org/hibernate/mapping/Column.html#setDefaultValue(java.lang.String )
        Hide
        Graeme Rocher added a comment -

        Moving non-critical issues that aren't going to make it into 1.1 to 1.2

        Show
        Graeme Rocher added a comment - Moving non-critical issues that aren't going to make it into 1.1 to 1.2
        Hide
        Miguel Almeida added a comment -

        This also happens if you have the following scenario:

        @Entity
        public class ConcretePlayer extends Card

        { private int contract; }

        @Entity
        @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
        public class Card

        { private int id; }

        Scaffolding these classes and trying to list/edit Card will throw:

        Caused by: org.springframework.orm.hibernate3.HibernateSystemException: Null value was assigned to a property of primitive type setter of fifa.player.ConcretePlayer.contract; nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of fifa.player.ConcretePlayer.contract

        If someone has an idea how to solve this problem, I'd be glad to hear!

        Show
        Miguel Almeida added a comment - This also happens if you have the following scenario: @Entity public class ConcretePlayer extends Card { private int contract; } @Entity @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public class Card { private int id; } Scaffolding these classes and trying to list/edit Card will throw: Caused by: org.springframework.orm.hibernate3.HibernateSystemException: Null value was assigned to a property of primitive type setter of fifa.player.ConcretePlayer.contract; nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of fifa.player.ConcretePlayer.contract If someone has an idea how to solve this problem, I'd be glad to hear!
        Hide
        Graeme Rocher added a comment -

        the solution is to use the non-primitive wrapper types (Integer instead of int)

        Show
        Graeme Rocher added a comment - the solution is to use the non-primitive wrapper types (Integer instead of int)

          People

          • Assignee:
            Graeme Rocher
            Reporter:
            Marc Palmer
          • Votes:
            5 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development