Announcement Announcement Module
No announcement yet.
Bidirectional One-To-One? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bidirectional One-To-One?

    i'm new to roo and just playing around with 1.1.5 release and the latest 1.2 snapshot... and i'm wondering, that it is possible to get bidirectional one-to-many & many-to-many relationships by means of the --mappedBy attribute, but i've found no way to setup bidirectional one-to-one relationships (especially in 1.2 snapshot)? i've searched the forum but unfortunately got no satisfying answer.

    imagine 2 entities, order and invoice. an order can have one invoice (in case of a correctly completed order), an invoice has to belong to one order.
    field reference --fieldName invoice --type ~.domain.Invoice --cardinality ONE_TO_ONE
    added to entity order manages the orders invoice, but what's about the other way round? or do i have to go the way round order, to find out to which order an invoice belongs to?

    as a workaround i've defined a one-to-many / many-to-one relationship and limited the field set to --sizeMin 0 and --sizeMax 1, but that should not be the way it goes... quite poor, don't you think
    and it does not avoid the possibility to give two different invoices the same order-id...?!

    any hints on that would be appreciated! THX!
    Last edited by tiny969; Sep 8th, 2011, 04:37 AM.

  • #2
    You're right; looking at the Roo source code (and the section of the reference docs that we automatically generate from it), the "mappedBy" option is only available for the "field set" command. This seems like an omission; please log an "improvement" ticket in JIRA to have this option added.

    As an alternative workaround, you can add a 1:1 field both ways, then manually add the "mappedBy" attribute to the inverse side of the relationship yourself; this should be neater than the workaround you described above.


    • #3
      thank you for your answer. i tried to add a --field reference both ways, following my example above. please correct me if i'm wrong:

      entity --class ~.domain.Order --testAutomatically
      field reference --fieldName invoice --type ~.domain.Invoice --cardinality ONE_TO_ONE
      entity --class ~.domain.Invoice --testAutomatically
      field reference --fieldName order --type ~.domain.Order --cardinality ONE_TO_ONE
      unfortunately while executing my script, i get the following exception when reaching the second field reference:
      java.lang.IllegalArgumentException: Invalid dependency between upstream 'MID:org.springframework.roo.addon.dod.DataOnDeman dMetadata#SRC_TEST_JAVA?[...].domain.OrderDataOnDemand' and downstream 'MID:org.springframework.roo.addon.dod.DataOnDeman dMetadata#SRC_TEST_JAVA?[...].domain.InvoiceDataOnDemand'

      as you can see, i do not have the chance to try to add --mappedBy to my 1:1 JPA manually, because execution of the script stops here...
      if i got that right, following ROO-2322, this should be fixed in 1.2.0 RC1? currently i'm using 1.2.0.BUILD-SNAPSHOT [rev b00913d] on win7.

      i will add the 1:1 mappedBy issue to jira, but would love to get a solution for this exception first... thx a lot again!

      created ROO-2725 for both issues... sorry for bad formatting
      Last edited by tiny969; Sep 9th, 2011, 12:56 AM.


      • #4
        Quick workaround: leave out "--testAutomatically" for one of the above entities.

        Long answer: I'll investigate further...


        • #5
          as you said, i left out "--testAutomatically" for invoice. script runs, tests are ok, i looked at & & added mappedBy in like

              private Order order;
              private Invoice invoice;
          so far, so good. next i tried web mvc and opened my browser....

          1. issue: created an order 1, next created an invoice A and linked order 1 to it. looked at order 1 and order 1 did not know about invoice A...! i chose invoice A in the order update form and clicked save:
          at java.lang.StringBuilder.append( 115)
          at [...].order.Order_Roo_ToString.ajc$interMethod$[...]_order_Order_Roo_ToString$[...]l_order_Order$toString(Order_Roo_ToString.aj:17)
          at [...].order.Order.toString(
          at java.lang.String.valueOf(

          same thing i get for Invoice toString, both referring to the getters in [...]_ToString.aj. did i miss something or is this just another bug?

          2. issue: M:N relationship, e.g. group - discount: a discount can belong to multiple groups and the other way round. created 2 discounts and linked them both to 2 groups. then tried to delete a discount, result: no discounts and no groups anymore... is there a way to influence cascading?

          just two more issues i've noticed during testing, but first it would be nice to have a bidirectional 1:1 including --testAutomatically and a --mappedBy for field reference
          i'd like to setup a quite complex model, but it seems that i have to do a lot of things manually at the moment...