3 Replies Latest reply on Feb 10, 2011 4:28 AM by Pablo Souza

    Understanding objects relations one-to-many and many-to-many with flex4

    pbesi Level 1

      Hello, I'm a newbie on Flex 4.

      I read several examples and also the "one week training". What I couldn't find is how to manage relations among objects.

       

      I understood that objects coming from the server must be translated to value objects.

      Because I'm working with Spring, Hibernate and BlazeDS, I've several beans and relations defined with the annotations @OneToMany and @ManyToMany. These relations are managed using List, Set and Map objects.

       

      More precisaly, I've to create a flex datagrid, each row is an "author" objects. Cliccking a row, I need to load some other related objects (let's call them "documents").

      Auhtor and Documents objects are in a one-to-many relation.

       

      My questions are:

      1) Which is the correct way to handle related objects in Flex? This questions arise from the fact that there are two ways in order to manage such objects. The first is to load the parent object and them call related method in order to get child. For example author.getDocuments(). I get all the related objects (author+ all his documents) with just one call, the one to load the "author" object. BlazeDS doesn't support lazy loading.

      The second way, is to provide the parent ID to a method that loads the related objects. For example getDocuments(authorID). I need to make a query for each ID

      Obviously, the value objects we have to create with AS are different. In the first case, the author VO will have a ArrayCollection in order to manage documents.In the second case I'll not create this property.

       

      2) In Flex, which is the best way? is it possible to manage objects (documents) of objects (authors) with MXML?

      3) More in general, Flex experts people, what are used to do? Where I can find an example?

       

       

      Thank you

      Paolo

        • 1. Re: Understanding objects relations one-to-many and many-to-many with flex4
          Pablo Souza Level 3

          Hi Paolo,

           

          Answering your questions:

           

          1) It depends on the amount of data. If I have just few authors and few related documents I prefer to retrieve all information in the same AuthorVO that has a list called documents.

           

          public class AuthorVO
          {
               public var documents:ArrayCollection;
          }
          

           

          But if I have a lot of authors or if I have a lot of related documents, I prefer to retrieve the AuthorVO and AuthorDocumentsVO apart.

           

          public class AuthorVO
          {
          }
          
          public class AuthorDocumentsVO
          {
          }
          


          2) I didn't understand this question. Could you explain me better?

           

          3) You can google a lot of examples using Flex and BlazeDS. I have a blog post about this but it's written only in Portuguese. Anyway you can download the Flex and Java projects in the last line of the post. Maybe it can help you: http://rectius.com.br/blog/?p=440

           

           

           

           

          Greets,

          Pablo Souza

          • 2. Re: Understanding objects relations one-to-many and many-to-many with flex4
            pbesi Level 1

            Thank you Pablo.

            Regarding my question n.2, it is confused.

             

            Here what I meant:

            Let's suppose I work with VO and ArrayCollection (the first "way" I wrote above). To get "Documents" objects, I need to call author.getDocuments().

             

            Now, I'm not sure that I'm able to do the same with MXML and that is is the easiest way. I think it should be easier working with "Documents" object instead of Auhors in order to work with Documents.

            For example, in order to get autors I can write a CallResponder and use a token. To fill a datagrid I define a method like

             

            protected function dataGrid_creationCompleteHandler(event:Flex:Event):void

            {

                 getAuthorsResult.token = auhtorService.getAuthors();

            }

             

            Now in order to fill the grid, I can use  for example <mx:DataGridColumn dataField="lastname">. Very easy and clear.

             

            But now, if I need to extract the Document objects and visualize them inside anothee DataGrid, I don't know how to do.

            For example, in <mx:DataGridColumn> tag what I've to write in order to visualize a Document property, using an Author object?

            Fixing an Auhtor, I need to loop over each Documents, but how can I loop inside the MXML? Something like <mx:DataGridColum dataField="author.documentloopvariable....>

             

            It should be better to get Documents via a method like getDocuments(authorID). Now I get a token related to a CallResponder, and I can work at the same manner as the Author. Isn't it? But this is the second "way" I wrote above.

             

            Well, I hope my question in a little clear.

            thank you

            Pbesi

             

             

             

             

             

             

             

            • 3. Re: Understanding objects relations one-to-many and many-to-many with flex4
              Pablo Souza Level 3

              Hi Pbesi,

               

               

              If you have an AuthorVO.as that has a list of documents:

               

              public class AuthorVO
              {
                   public var authorId:String;
                   public var documents:ArrayCollection; // array of AuthorDocumentsVO
              }
              

               

              the var documents can be a list of this AuthorDocumentsVO for instance:

               

              public class AuthorDocumentsVO
              {
                   public var documentAuthor:String;
                   public var documentId:String;
                   public var documentName:String;
                   public var documentType:String;
              }
              

               

              you can assign this list as the dataprovider of the Documents' datagrid:

               

              <mx:DataGrid id="gridDocuments" dataProvider="{authorVO.documents}">
                  <mx:columns>
                          <mx:DataGridColumn dataField="documentID" />
                          <mx:DataGridColumn dataField="documentName" />
                          <mx:DataGridColumn dataField="documentType" />
                          <mx:DataGridColumn dataField="documentAuthor" />
                  </mx:columns>
              </mx:DataGrid>

               

               

              Greets,

              Pablo Souza