Results 1 to 5 of 5

Thread: Bidirectional One-To-One?

  1. #1
    Join Date
    Sep 2011
    Posts
    6

    Default Bidirectional One-To-One?

    hi,
    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.
    Code:
    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 at 04:37 AM.

  2. #2
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    668

    Default

    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. #3
    Join Date
    Sep 2011
    Posts
    6

    Default

    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:

    Code:
    entity --class ~.domain.Order --testAutomatically
    [...fields...]
    field reference --fieldName invoice --type ~.domain.Invoice --cardinality ONE_TO_ONE
    entity --class ~.domain.Invoice --testAutomatically
    [...fields...]
    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!

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

  4. #4
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    668

    Default

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

    Long answer: I'll investigate further...

  5. #5
    Join Date
    Sep 2011
    Posts
    6

    Default

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

    Code:
    //Invoice.java
    @OneToOne(mappedBy="invoice")
        private Order order;
    
    //Order.java
    @OneToOne
        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(StringBuilder.java: 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(Order.java:1)
    at java.lang.String.valueOf(String.java:2826)

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

Posting Permissions

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