Results 1 to 6 of 6

Thread: Adding multiple child records from a parent object (One-To-Many)

  1. #1
    Join Date
    Feb 2013
    Posts
    15

    Default Adding multiple child records from a parent object (One-To-Many)

    I'm currently working on a roo prototype which isn't far from the pizzashop example.

    I have a simple example of a one-to-many relationship (orders and order items).

    It seems that the auto-generated user interface (web-mvc) expects you to perform your one-to-many relationship association from the child record (associate the one from the many). What I would like to do is add multiple child order items from the order record after the order record has been created. That way I could easily see all order items associated with this order.

    Two Questions....
    1. Is there an example I'm not seeing where roo would create this type of UI for a one-to-many relationship where I can add multiple children from the parent record?

    2. What's the simplest approach for doing this? I'm thinking of a few options....

    a. Modifying the JSP code to show the child records.
    b. Creating some kind of master-detail view to allow the user to add multiple child records from the parent.

  2. #2
    Join Date
    Jan 2010
    Location
    Mislata - Valencia - Spain
    Posts
    162

    Default

    Quote Originally Posted by FreeCoffee View Post
    Is there an example I'm not seeing where roo would create this type of UI for a one-to-many relationship where I can add multiple children from the parent record?
    You have available de gvNIX patterns add-on:

    https://code.google.com/p/gvnix/wiki/DocPattern

    Regards !
    Mario Martínez Sánchez
    Project Manager & Software Architect
    --------------------------
    Disid Technologies S.L.
    http://www.disid.com
    --------------------------
    gvNIX
    http://gvnix.googlecode.com
    http://www.gvnix.org

  3. #3
    Join Date
    Feb 2013
    Posts
    15

    Default

    Thanks Martinez. I'll check this framework out. Meanwhile if anyone else has high-level guidance let me know.

    Thanks everyone.

  4. #4
    Join Date
    Jun 2008
    Location
    Philadelphia, PA, USA
    Posts
    212

    Default

    Good news is, it's just a Spring/JPA application. The big question is whether you have a bi-directional or uni-directional one-to-many. In other words, do you have just a @OneToMany or a reverse-ended @ManyToOne. If you have both directions, and you use 'mappedBy' in the one-side, that means the many side (child) is in charge of getting JPA to persist things.

    So, you'd do this:

    bi-directional one-to-many -
    - add the child to the collection (satisfies Java)
    - set the reference to the parent in the child (also satisfies JPA's dirty checking)

    uni-directional one-to-many
    - add the child into the collection (since it's unidirectional, the parent is in charge, and it will add the row(s))

    I usually do the bi-directional relationships because if I add one, I don't want to cause the entire collection to be recalculated. Roo is doing the same thing - by having you edit the child, you are only doing one insert, and it doesn't have to update a collection for the one-side.

    Regardless, you'll be looking at doing some JPA code - please do this inside of a service object in a method marked as @Transactional so that you get it all done by one connection/transaction (or in the active-record class for the parent, just call both the add to the collection and set the reference to the parent in the child).

    You'll also have to code the controller feature yourself. Take a good look at the JSPX tags, and you can probably make your own where necessary to handle some of the more complex tasks.
    Ken Rimple
    Chariot Solutions
    email: krimple@chariotsolutions.com
    work: www.chariotsolutions.com/education
    personal: www.rimple.com

    Author: Spring Roo in Action (Manning)
    MEAP Site: manning.com/rimple

  5. #5
    Join Date
    Feb 2013
    Posts
    15

    Default

    Ken,

    Cool, thanks for the response. I've been reading your book by the way. I'm coming from the .net world so I'm new to java persistence. I'll try to make a decision about bi-directional or uni-directional and go from there. I think I understand what you're saying. I suppose part of the issue would be to determine if the user should save the parent record before the children are added to the collection.

    I just wanted a sanity check that there wasn't a quick way to configure these types of one-to-many relationship.

    I'll let you know how it goes.

  6. #6
    Join Date
    Feb 2013
    Posts
    10

    Default

    gvNix did all the master / detail UI, just with:

    web mvc pattern master --class ~.web.OwnerController --name MyPattern --type register
    web mvc pattern detail --class ~.web.OwnerController --name MyPattern --type tabular --field pets

    as in https://code.google.com/p/gvnix/wiki/DocPattern


    i had some trouble to install gvnix, since i couldnt find it in addon search, so i installed manually.

Tags for this Thread

Posting Permissions

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