8 Replies Latest reply on Jul 10, 2006 5:10 PM by Andy Howse

    Is Master-Detail possible in Seam

    Andy Howse Newbie

      I'm trying to write a page that has the parent details and then a list of children rows.

      The page allows you to amend the parent details, modify, delete or add child rows. All actions are within the same page and I want all actions to be part of the same transaction.

      It does pretty much work, but I have a few funnies. For example, I add a new child row, save and then perform the same action again, seam will insert the previously inserted child row again.

      I can't find an example that shows if and how this can be done. Is it possble ?

        • 1. Re: Is Master-Detail possible in Seam
          Pete Muir Master

          Yes it is.

          Perhaps if you post code and where you are having problems someone can help?

          The problem you mention, duplicate entry of child row: perhaps you aren't creating a new child object for the second add?

          I have such a page, it is based on a generic superclass that provides the standard actions and can be extended as needed to provide extra functionality and it works as you describe.

          • 2. Re: Is Master-Detail possible in Seam
            Andy Howse Newbie

            This is the parent code:

            @Stateful
            @Name("eventTypeUpdate")
            @Conversational(ifNotBegunOutcome = "eventtypefind")
            @LoggedIn
            public class EventTypeUpdateAction implements EventTypeUpdate {

            @PersistenceContext
            private EntityManager em;

            @In(required = false)
            @Out
            private EventType eventType;

            @In(create = true)
            private transient FacesMessages facesMessages;

            @In(create = true)
            private transient Events events;

            @In
            private EventTypeFind eventTypeFind = null;

            @Begin
            public String selectEventType() {
            this.eventType = this.eventTypeFind.getSelectedEventType();
            return "eventtypeupdate";
            }

            @End
            public String cancel() {
            return "eventtypefind";
            }

            @End
            public String update() {
            List existing = em.createQuery(
            "select name from EventType where name=:name and id <> :id")
            .setParameter("name", eventType.getName()).setParameter("id",
            eventType.getId()).getResultList();
            if (existing.size() == 0) {
            em.merge(this.eventType);
            Events.instance().raiseEvent("eventTypeUpdate");
            return "eventtypefind";
            } else {
            facesMessages.add("An Event Type already exists with name #{eventtype.name}");
            return null;
            }
            }

            public String newEventTypeRole() {
            EventTypeRole etr = new EventTypeRole();
            this.eventType.getEventTypeRoles().add(etr);
            etr.setEventType(this.eventType);
            return "eventtypeupdate";
            }

            public int getEventTypeRolesNo() {
            return this.eventType.getEventTypeRoles().size();
            }

            @DataModel
            public List getEventTypeRoles() {
            if (this.eventType == null || this.eventType.getEventTypeRoles() == null)
            return new ArrayList();
            return this.eventType.getEventTypeRoles();
            }

            @DataModelSelection
            private EventTypeRole selectedEventTypeRole;

            public EventTypeRole getSelectedEventTypeRole() {
            return this.selectedEventTypeRole;
            }

            public String deleteEventTypeRole() {
            if (this.eventType.getEventTypeRoles().contains(this.getSelectedEventTypeRole())) {
            EventTypeRole etr = this.getSelectedEventTypeRole();
            if (etr.getId() != null && etr.getId() > 0)
            em.remove(etr);
            this.eventType.getEventTypeRoles().remove(etr);
            em.merge(this.eventType);
            }
            return "eventtypeupdate";
            }

            @Destroy
            @Remove
            public void destroy() {
            }
            }

            • 3. Re: Is Master-Detail possible in Seam
              Andy Howse Newbie

              Sorry mean't action code.

              I've not posted the entities as I don't think there is anthing unusual there.

              • 4. Re: Is Master-Detail possible in Seam
                Andy Howse Newbie

                OK seem to have solved the repeating inserts. Adding a scope=PAGE on the @DataModel of the FindAction did the trick. Not sure why...

                The next problem is that when I delete a child row, the action is persisted to the db. Is there a way to stop this. I've been digging around for stuff on Transaction annotations, but not found anything good.

                • 5. Re: Is Master-Detail possible in Seam
                  Pete Muir Master

                  @TransactionAttribute(TransactionType.NOT_SUPPORTED)

                  FlushMode.NEVER got removed from the EJB3 spec unfortunately, I know hibernate were going to provide it anway but it doesn't seem to be there atm.

                  • 6. Re: Is Master-Detail possible in Seam
                    Jonatan Calvert Newbie

                    petemuir, can you elaborate on this? What does that do, TransactionType.NOT_SUPPORTED? Does it actually affect flushing?

                    I'm interested in potentially converting a Struts app backed by Hibernate to Seam. However we've had to set FlushMode.NEVER in many places because of the nature of our transactions. I'd like to believe there's a way to code around that, but I'd hate to start churning out code only to find it simply won't work.

                    • 7. Re: Is Master-Detail possible in Seam
                      Gavin King Master

                       

                      "FrustratedBySeam" wrote:
                      The next problem is that when I delete a child row, the action is persisted to the db. Is there a way to stop this.


                      One way to stop this is to use TransactionType.NOT_SUPPORTED

                      • 8. Re: Is Master-Detail possible in Seam
                        Andy Howse Newbie

                        I think I've got it all working now with a few tweaks and using TransactionType.NOT_SUPPORTED.

                        Thanks for the comments.

                        Seams looking good once more.....