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

    How to add two entities home in the same page?

    Frederico Benevides Newbie


      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:outputText value="Add new Person"
       <h:form id="person">
       <h:panelGrid columns="2">
       <h:outputLabel for="name">
       <s:decorate id="nameDecorator">
       <h:inputText value="#{personHome.instance.name}" required="true"/>
       <h:outputLabel for="age">
       <s:decorate id="ageDecorator">
       <h:inputText value="#{personHome.instance.age}" required="true"/>
       <h:outputLabel for="house">
       <s:decorate id="numberDecorator">
       <h:inputText value="#{phoneHome.instance.number}"/>
       <h:commandButton action="#{personHome.persist}" value="Save"/>
       <h:commandButton action="#{personHome.remove}" value="Delete"/>

      My PersonHome
      public class PersonHome extends EntityHome<Person> {
       public void setPersonId(Integer id) {
       public Integer getPersonId() {
       return (Integer)getId();
       protected Person createInstance() {
       Person person = new Person();
       return person;

      My phoneHome

      public class PhoneHome extends EntityHome<Phone> {
       public void setPhoneId(Integer id) {
       public Integer getPhoneId() {
       return (Integer)getId();
       protected Phone createInstance() {
       Phone phone = new Phone();
       return phone;

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

      Thank you,


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

          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)