Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Does Roo support n:m mappings ?

  1. #1
    Join Date
    Aug 2005
    Posts
    3

    Default 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. #2
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    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

  3. #3

    Default

    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.

  4. #4
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    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

  5. #5
    Join Date
    Nov 2009
    Posts
    2

    Default

    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

  6. #6
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    @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

  7. #7
    Join Date
    Nov 2009
    Posts
    2

    Default

    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

  8. #8

    Default

    @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??

  9. #9
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    Can you share your script so we can see what exactly you are seeing in your application?
    Stefan Schmidt
    Software Engineer, Spring Roo
    SpringSource - a division of VMware
    twitter @schmidtstefan

  10. #10

    Unhappy 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •