Announcement Announcement Module
Collapse
No announcement yet.
Does Roo support n:m mappings ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Does Roo support n:m mappings ?

    I'm quite fond of the features Roo M2 provides. While looking at the samples I did not find a sample of a n:m mapping - How would that work in Roo ?

    Regards.
    Paul

  • #2
    Hi Paul,

    Roo currently offers two commands to generate JPA relationships:

    Code:
    add field reference
    which generates a @ManyToOne JPA relationship mapping, and

    Code:
    add field set
    which generates a @OneToMany JPA relationship.

    Take a look at the clinic.roo script to see how to use them.

    So to get a n:m relationship you would typically create three tables:

    Order
    -OneToMany->OrderOrderLine

    OrderOrderLine
    -orders @ManyToOne->Order
    -orderlines @ManyToOne->OrderLine

    OrderLine
    -OneToMany->OrderOrderLine

    Hope this helps.

    -Stefan

    Comment


    • #3
      Great thread thanks alot... I've been stumbling on when to use 'add field set' and 'add field reference' ever since I began using Roo. Now if I can only figure out the most straightforward way of making a 1:1 relationship by using Roo I'll be all set.

      Comment


      • #4
        Hi Paul,

        Basically you are already using the right command for this . Just use the 'add field reference' command to essentially create a one-to-one mapping as seen in the clinic.roo script.

        On the MVC side this will create a dop-down list where you can choose only one element as opposed to the 'add field set' which creates a multi-selection box on the MVC side.

        Hope this helps.

        -Stefan

        Comment


        • #5
          Hi Stefan,

          would you mind pasting the roo commands for this n:m mapping, I am struggeling to get this done here. Basically I want to have a community which has a number of persons in it and a person who can be member of different communities. But I am not sure how to write this in roo.

          Thanks,
          Philipp

          Comment


          • #6
            @pbussche,

            Below the script for a true n:m mapping:

            Code:
            project --topLevelPackage com.foo.opensource
            persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
            entity --name ~.domain.Community --testAutomatically
            field string name
            entity --name ~.domain.Person --testAutomatically
            field string name
            field set --fieldName communities --element ~.domain.Community --cardinality MANY_TO_MANY 
            field set --element ~.domain.Person --fieldName members --class ~.domain.Community --cardinality MANY_TO_MANY 
            controller all --package ~.web
            I hope this helps .

            Cheers,
            Stefan

            Comment


            • #7
              Cool, thanks. That works.
              But it seems now that I have to store on the community side which person is part of it and on the person side which community he is a member of. Well I overwrote one setter method to automatically do that when a person is created and a community is specified but is that the recommended way of doing it ?
              I guess this is more persistence basics than roo, sorry

              Comment


              • #8
                @Stefan
                ...
                I'm trying ROO. Tutorial studying and so on.
                Your example is exactly what I need now, but... it doesn't work for me!
                I mean:
                - i create three person: A, B and C;
                - i create two communities: X and Y;
                - i associate A and B to X and C to Y;
                - from the communities list, I can't see the association done.

                It looks like data isn't consistent. Can you help me please??

                Comment


                • #9
                  Can you share your script so we can see what exactly you are seeing in your application?

                  Comment


                  • #10
                    My script

                    Thanks for your reply Stefan!
                    Code:
                    // Spring Roo 1.0.0.RELEASE [rev 564] log opened at 2010-01-12 22:15:57
                    project --topLevelPackage it.celi
                    persistence setup --provider HIBERNATE --database HYPERSONIC_PERSISTENT
                    database properties list
                    database properties set --key database.url --value jdbc:hsqldb:${user.home}/dd4-cloe-roo
                    database properties list
                    entity --class ~.domain.BusinessFunction
                    field string code --notNull --sizeMin 1 --sizeMax 30
                    field string specialRequests --sizeMax 100
                    test integration
                    controller scaffold ~.web.BusinessFunctionController
                    selenium test --controller ~.web.BusinessFunctionController
                    perform test
                    // script --file /home/fabio/wedding.roo
                    quit
                    // Spring Roo 1.0.0.RELEASE [rev 564] log closed at 2010-01-12 22:17:36
                    // Spring Roo 1.0.0.RELEASE [rev 564] log opened at 2010-01-12 22:24:42
                    entity --class ~.domain.UserRole
                    field string --fieldName id --notNull --sizeMin 1 --sizeMax 30
                    field string --fieldName description --sizeMax 100
                    field set --fieldName allowedFunctions --element ~.domain.BusinessFunction 
                    quit
                    // Spring Roo 1.0.0.RELEASE [rev 564] log closed at 2010-01-12 22:34:43
                    // Spring Roo 1.0.0.RELEASE [rev 564] log opened at 2010-01-12 22:36:51
                    controller scaffold --entity ~.domain.UserRole --class ~.web.UserRoleController
                    selenium test --controller ~.web.UserRoleController
                    quit
                    // Spring Roo 1.0.0.RELEASE [rev 564] log closed at 2010-01-12 22:39:37
                    // Spring Roo 1.0.0.RELEASE [rev 564] log opened at 2010-01-12 22:45:51
                    perform eclipse
                    quit
                    // Spring Roo 1.0.0.RELEASE [rev 564] log closed at 2010-01-12 22:59:29
                    Ok with BusinessFunction now.
                    Then... I create a UserRole. I update a field, like description. I list all roles and....... roo duplicates it. I find the 'old' with his relations with BusinessFunctions and the new one.
                    I can't understand.

                    Comment


                    • #12
                      Yes you are on the right track. Your UserRole type uses a field named 'id' as part of the type definition. This field name is also the field name Roo chooses by default as a JPA @Id. Therefore, you see that the updates to UserRole are handled different to what you would normally expect. You have two choices to address this issue:

                      1. Rename your id field to something other than id (ie roleId). This way Roo will still choose id as the identifier for persistence purposes and roleId becomes independent of that.

                      2. Change the default name of the identifier in your UserRole type:

                      Code:
                      @RooEntity(identifierField="persistenceId")  
                      public class UserRole{...}
                      This way you can retain your 'id' field name and instruct Roo to use the 'persistenceId' (or any other name) field for its internal JPA purposes.

                      If you use any of these options your application should work fine.

                      HTH

                      Comment


                      • #13
                        Hi Stefen,

                        I have been trying previous code and it was not working for me. Did some changes and now I can see both side of the many-to-many updated

                        Code:
                        project --topLevelPackage com.foo.opensource
                        persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
                        
                        entity --class ~.domain.Community --testAutomatically
                        field string name 
                        entity --class ~.domain.Person --testAutomatically
                        field string name 
                        field set --fieldName communities --element ~.domain.Community --cardinality MANY_TO_MANY
                        field set --fieldName members         --element ~.domain.Person       --class ~.domain.Community --cardinality MANY_TO_MANY --mappedBy communities
                        controller all --package ~.web
                        Missing part being --mappedBy communities

                        Unfortunately, I can only add communities to poeple and not the opposite from the UI. Still something missing

                        --Marc

                        Comment


                        • #14
                          After looking at hibernate doc here: http://docs.jboss.org/hibernate/core...-bidirectional

                          found that "Changes made only to the inverse end of the association are not persisted."

                          so that explains why I can't add items from both sides of the association.

                          Comment

                          Working...
                          X