How to add two entities home in the same page?
fredbene Mar 7, 2007 11:44 AMHello,
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