1 Reply Latest reply on Mar 8, 2007 2:32 PM by cja987

    How to add two entities home in the same page?

    fredbene

      Hello,

      I have my page that I want to add a person and his phone.

      I would like to know what I need to do when I call my #{personHome.persist} save my #{personHome.instance.name} , #{personHome.instance.age} WITH #{phoneHome.instance.number} as you can see in my page.


      <ui:define name="body">
      
       <h:messages/>
      
       <center>
       <h:outputText value="Add new Person"
       rendered="#{!personHome.managed}"/>
       </center>
      
       <h:form id="person">
      
       <s:validateAll>
       <h:panelGrid columns="2">
       <h:outputLabel for="name">
       Name:
       </h:outputLabel>
       <s:decorate id="nameDecorator">
       <h:inputText value="#{personHome.instance.name}" required="true"/>
       </s:decorate>
      
       <h:outputLabel for="age">
       Age:
       </h:outputLabel>
       <s:decorate id="ageDecorator">
       <h:inputText value="#{personHome.instance.age}" required="true"/>
       </s:decorate>
      
       <h:outputLabel for="house">
       Phone:
       </h:outputLabel>
      
       <s:decorate id="numberDecorator">
       <h:inputText value="#{phoneHome.instance.number}"/>
       </s:decorate>
      
      
       </h:panelGrid>
       </s:validateAll>
       <center>
       <h:commandButton action="#{personHome.persist}" value="Save"/>
       <h:commandButton action="#{personHome.remove}" value="Delete"/>
       </center>
      
       </h:form>
      
      </ui:define>
      


      My PersonHome
      @Name("personHome")
      public class PersonHome extends EntityHome<Person> {
      
       public void setPersonId(Integer id) {
       setId(id);
       }
      
       public Integer getPersonId() {
       return (Integer)getId();
       }
      
       @Override
       protected Person createInstance() {
       Person person = new Person();
       return person;
       }
      }
      
      


      My phoneHome

      @Name("phoneHome")
      public class PhoneHome extends EntityHome<Phone> {
      
       public void setPhoneId(Integer id) {
       setId(id);
       }
      
       public Integer getPhoneId() {
       return (Integer)getId();
       }
      
       @Override
       protected Phone createInstance() {
       Phone phone = new Phone();
       return phone;
       }
      }
      


      And how is mappend my Person and Phone
      
      Person:
      
      @OneToMany(mappedBy="owner", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
       public List<Phone> getPhones() {
       return phones;
       }
       public void setPhones(List<Phone> phones) {
       this.phones = phones;
       }
      
      
      Phones
      
      @ManyToOne(fetch=FetchType.LAZY)
       @JoinColumn(name="person_id", nullable=false)
       public Person getowner() {
       return owner;
       }
       public void setOwner(Person owner) {
       this.owner = owner;
       }
      


      Thank you,

      Frederico

        • 1. Re: How to add two entities home in the same page?

          You could make personHome inject the current phone, then have personHome.persist() call getInstance().phones.add(phone)

          However, that wires the policy a little too low level for my tastes. I personally recommend an EditPersonAction (or whatever you want to call it) to back your edit page that has personHome and phoneHome instances, and has a .save() action method that manages the relationship there, rather than wiring controller-specific actions into the data access layer.

          (I do break this rule in one place, and that's when injecting an "author" field into new instances, because the user identity is always in scope anyway, and I don't want anything escaping that policy)