5 Replies Latest reply on Nov 6, 2016 4:05 PM by maurizio.giacomelli

    Any recommended strategy for Angular/REST scaffolds with JPA bidirectional relationships

    mcasperson

      I recently attempted to migrate from the JSF to the Angular UI scaffolds, but I could not find a nice solution that would allow Jackson to serialize bidirectional relationships.

       

      The @JsonManagedReference and @JsonBackReference annotations (http://wiki.fasterxml.com/JacksonFeatureBiDirReferences) worked well in preventing infinite loops during serialization, but unfortunately because anything annotated with @JsonBackReference is not serialized, the scaffold UI ended up showing incomplete information.

       

      Is there a quickstart or something similar that demonstrates entities with bidirectional relationships displayed by the Angular Scaffold?

        • 1. Re: Any recommended strategy for Angular/REST scaffolds with JPA bidirectional relationships
          gastaldi

          One strategy is to create DTOs with the data that you want to be JSONified. We use that strategy in Forge.

          Vinnet Reynolds may be able to tell more about it.

          • 2. Re: Any recommended strategy for Angular/REST scaffolds with JPA bidirectional relationships
            vineet.reynolds

            Well, we recommend using the DTO strategy for this. Even with @JSONManagedReference and @JSONBackReference annotateds on JPA entities, you would run into some issues, especially when updating the entities via a HTTP PUT. That's why we recommend using DTOs.

             

            For now, with the DTO strategy, Forge supports creation of a 2-level DTO hierarchy where no cycles will be created in the DTO graph, thus eliminating the need for Jackson annotations, while supporting updates to the entities in a limited form. For every DTO that it creates, Forge will traverse the corresponding JPA entity and map it's properties; it will also traverse the relationships in the JPA entities and create nested DTOs to represent the related entities. It does not however traverse any further, primarily to break any cyclic or bi-directional relationships. The constructed DTOs contain logic to assemble the JPA entity back from the contents of the DTO, and also update any object references (so it gets past the problems you run into with some of the Jackson annotations); it does not contain logic to update properties of referenced JPA entities (say you delink B from A, then the resource for A will have no issues in delinking the objects, but if you update B and expect the REST resource for A to transitively update B, then this is not supported).

             

            If you need additional strategies to be built, we can help with that.

            • 3. Re: Any recommended strategy for Angular/REST scaffolds with JPA bidirectional relationships
              mcasperson

              So I may have been running rest-generate-endpoints-from-entities instead of letting the Angular plugin generate these for me (I just looked at the instructions at http://forge.jboss.org/addon/org.jboss.forge.addon:angularjs again and saw it doesn't ask you to run rest-generate-endpoints-from-entities).

               

              I'll try again following the instructions more closely and check the results.

              • 4. Re: Any recommended strategy for Angular/REST scaffolds with JPA bidirectional relationships
                vineet.reynolds

                Ah, sorry about that (and for the late reply). The instructions are a bit out of date. REST resource generation is now optional in the angularjs addon. It gets activated with the generateRestResources flag, like so:

                 

                scaffold-generate --provider AngularJS --targets com.acme.model.Customer --generateRestResources --generator ROOT_AND_NESTED_DTO ....

                • 5. Re: Any recommended strategy for Angular/REST scaffolds with JPA bidirectional relationships
                  maurizio.giacomelli

                  Try with

                  @JsonIdentityInfo(

                     generator = ObjectIdGenerators.PropertyGenerator.class,

                     property = "id")

                  at the top of declaration of all the 2 class of relations. (this may improve performance if you use lazy or eager mapping)

                   

                  ...or if you don't need al data for both relations entity

                  you can use

                  @JsonIgnore on the mapping of your type(es:"many to one")

                   

                  you can found many example in google if you use my keywords,