0 Replies Latest reply on Sep 12, 2006 3:51 AM by Abhilash Kumar

    Maintain object associations during edit

    Abhilash Kumar Newbie

      Hello :

      What is the best way to maintain object associations in Seam applications ? Let me give a scenario.

      I have a User entity bean and a UserDetail entity bean with one to many relationship. (There can be multiple entries in UserDetail table for one User ).

      My view has input texts for all the attributes of UserDetail entity bean followed by a Save button and a data table for listing the existing UserDetail objects. This page is used for creating a new UserDetail, editing an existing UserDetail and deleting a UserDetail. The UserDetail entity bean is bijected in the action method for save button.

      When the page is requested, a Factory method is called to populate the list of UserDetail objects in the datatable, if any. The datatable has edit and delete buttons in each row.

      This is what i do in edit button action
      1. Assign the DataModelSelection to UserDetail which has In and Out annotations.

      so that attributes of the clicked UserDetail row in copied to the create or edit area. Then when the save buttton is clicked, in the action method

      1. Check wheter the injected UserDetail.getId is null or not
      2. If null, em.persist
      3. If not null, em.merge

      Only one problem. The association between the UserDetail to User is lost since the user_id in user_detail table is set to null while updating.

      I tried to solve this problem by placing a hidden input with binding to USER_DETAIL.USER.ID . On clicking edit button, the value for this is set correctly but on clicking on save button i am getting a conversion error even before my action method is called.

      The immediate solution that comes to my mind is to fetch the UserDetail again in the action method for save (if id is not null) and copy the attributes of the injected UserDetail to it. But I am wondering any other elegant solutions are there for this problem.

      Any pointers will be really helpful.

      thanks and regards,

      -- Kannan