Spring Roo
  1. Spring Roo
  2. ROO-2716

roo json mappings are missing set/reference relationship methods

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 1.1.5.RELEASE, 1.2.0.M1
    • Fix Version/s: None
    • Component/s: WEB MVC
    • Environment:
      OSX Lion, but that is irrelevant for this issue, the same behavior is present on Linux, etc.

      Description

      When a pair of tables have a parent/child relationship, the REST conventions dictate that a list of child elements belonging to a parent should be exposed as a rest method.

      If one has:

      /api/parent - list of parents
      /api/parent/

      {parentId} - specific parent
      /api/children - list of all children regardless of parent
      /api/children/{childId} - specific child

      Where there is a set/reference ONE_TO_MANY bidirectional relationship between parent and child, one should also generate

      /api/parent/{parentId}

      /children - list of children belonging to parentId

      as an exposed REST method that returns the list of children belonging to a parent.

      Currently roo does not do this. At least. This appears to be the one missing element in the JSON API generation.

      Here's my test case:

      project com.test.foo
      dependency add --groupId org.codehaus.jackson --artifactId jackson-jaxrs --version 1.8.4
      jpa setup --provider HIBERNATE --database POSTGRES --databaseName cm --hostName localhost --userName admin --password fooo2 --transactionManager

      entity --class ~.domain.Parent --testAutomatically
      field string --fieldName name
      entity --class ~.domain.Child --testAutomatically
      field string --fieldName name

      // Setup heirarchical references
      focus --class ~.domain.Parent
      field set --fieldName children --type ~.domain.Child --cardinality ONE_TO_MANY --mappedBy parent
      focus --class ~.domain.Child
      field reference --fieldName parent --type ~.domain.Parent --cardinality MANY_TO_ONE

      json all --deepSerialize
      web mvc json setup
      web mvc json all
      perform eclipse

        Activity

        Hide
        Bruce Edge added a comment -

        If someone could give me some pointers, I could take a crack at this. I need to write lots of additional controller methods otherwise.
        Thanks in advance.

        -Bruce

        Show
        Bruce Edge added a comment - If someone could give me some pointers, I could take a crack at this. I need to write lots of additional controller methods otherwise. Thanks in advance. -Bruce
        Hide
        Stefan Schmidt added a comment -

        Hi Bruce,

        The methods in the Json controller ITDs are being produced by a type called WebJsonMetadata in Roo:
        https://github.com/SpringSource/spring-roo/blob/master/addon-web-mvc-controller/src/main/java/org/springframework/roo/addon/web/mvc/controller/json/WebJsonMetadata.java

        There you can see how the method signatures and their contents are produced. This is not trivial to do, but it does illustrate how things are constructed by Roo.

        Show
        Stefan Schmidt added a comment - Hi Bruce, The methods in the Json controller ITDs are being produced by a type called WebJsonMetadata in Roo: https://github.com/SpringSource/spring-roo/blob/master/addon-web-mvc-controller/src/main/java/org/springframework/roo/addon/web/mvc/controller/json/WebJsonMetadata.java There you can see how the method signatures and their contents are produced. This is not trivial to do, but it does illustrate how things are constructed by Roo.
        Hide
        Bruce Edge added a comment -

        I see what you mean by "not trivial"

        Thanks for the info.

        Show
        Bruce Edge added a comment - I see what you mean by "not trivial" Thanks for the info.
        Hide
        Rick Marry added a comment - - edited

        I think that I've figured out how to add a child to a parent set, and have listed it here:
        http://forum.springsource.org/showthread.php?114268-roo-json-mappings-are-missing-set-reference-relationship-methods&p=411033#post411033

        Now I'm trying to figure out how to update the child using PUT, specifically, how to pass in the parent reference with the PUT call.
        Is this something that works out of the box with ROO, or will I need to code up a special Controller method for that as well?

        Show
        Rick Marry added a comment - - edited I think that I've figured out how to add a child to a parent set, and have listed it here: http://forum.springsource.org/showthread.php?114268-roo-json-mappings-are-missing-set-reference-relationship-methods&p=411033#post411033 Now I'm trying to figure out how to update the child using PUT, specifically, how to pass in the parent reference with the PUT call. Is this something that works out of the box with ROO, or will I need to code up a special Controller method for that as well?
        Hide
        Oliver Lade added a comment -

        This is probably more of a request for Flexjson, but would it be possible for the deserialisation to convert an integer into an entity reference by assuming it's an ID and looking up an appropriate finder? It seems like it should be fairly possible, given the tools available. Perhaps it could be done as preprocessing in the controller, except that Flexjson seems to do all the type binding stuff internally.

        When creating an entity with a JSON POST, I'm passing in an array of IDs for a referenced entities set, except I get hit with a rather unintelligible error "java.lang.Integer cannot be cast to java.util.Map", which I've traced to flexjson.factories.SetObjectFactory, line 13. Once it's that deep perhaps it's too late for Roo to do much about it.

        Show
        Oliver Lade added a comment - This is probably more of a request for Flexjson, but would it be possible for the deserialisation to convert an integer into an entity reference by assuming it's an ID and looking up an appropriate finder? It seems like it should be fairly possible, given the tools available. Perhaps it could be done as preprocessing in the controller, except that Flexjson seems to do all the type binding stuff internally. When creating an entity with a JSON POST, I'm passing in an array of IDs for a referenced entities set, except I get hit with a rather unintelligible error "java.lang.Integer cannot be cast to java.util.Map" , which I've traced to flexjson.factories.SetObjectFactory, line 13. Once it's that deep perhaps it's too late for Roo to do much about it.

          People

          • Assignee:
            Unassigned
            Reporter:
            Bruce Edge
          • Votes:
            5 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

            • Created:
              Updated: