How to submit 2 form fields with same name
valatharv Oct 2, 2008 5:06 PMI am new to seam not sure how to submit 2 form fields with same name, Please suggest, thanks in advance...
We have one form which corresponds to different fields from different entities.
Root is Study entity, which have OneToMany relation to QExp, QExp have OneToMany realtion with Region, all the mapping is fine.
If there is ONE Region, i.e. one field like Pname, I am able to persist it.
Issue comes up when we provide multiple form fields with same name for Region, (e.g. if there are 2 Pnames and both have value "#{regionHome.instance.pname}")
After clicking "Save" values for Region 2 (PName) field is only persisted, which somehow overrides Region 1 (PName) field, it should svae both.
We have a Entity "Region" and corresponding "regionhome", value for each region form field in xhtml is mapped to regionHome.instance.fieldname
study.xhtml & studyHome.java does all the processing, studyHome.persist method is called on clicking save from study.xhtml.
XHTML file :
-----------
<h:form id="studyForm">
<rich:panel>
<f:facet name="header">Region 1</f:facet>
<s:decorate id="pnameDecoration" template="layout/edit.xhtml">
<ui:define name="label">PName</ui:define>
<h:inputText id="name" required="true"
value="#{regionHome.instance.pname}"/>
</s:decorate>
</rich:panel>
<rich:panel>
<f:facet name="header">Region 2</f:facet>
<s:decorate id="pnameDecoration" template="layout/edit.xhtml">
<ui:define name="label">PName</ui:define>
<h:inputText id="name" required="true"
value="#{regionHome.instance.pname}"/>
</s:decorate>
</rich:panel>
<h:commandButton id="save" value="Save" action="#{studyHome.persist}" rendered="#{!studyHome.managed}"/>
</h:form>
studyHome :
-----------
@Name("studyHome")
public class StudyHome extends EntityHome<Study>
{............
-----------
public String persist(){
List<QExp> qList = new ArrayList<QExp>();
qList.add(getQExp());
initStudy().setQExp(qList);
return super.persist();
}
public QExp getQExp() {
QExp quantExp = qExpHome.initQExp();
quantExp.setRegion(getRegion());
return quantExp;
}
public List<Region> getRegion() {
List<Region> regionList=new ArrayList<Region>();
Region region = regionHome.initRegion();
region.setTreatment(getTreatment());
regionList.add(region);
return regionList;
}
Study entity :
--------------
@Entity(name = "Study")
public class Study
implements Equals, HashCode, ToString
{......
protected List<QExp> qExp;
@JoinTable(.....)
public List<QExp> getQExp() {
if (qExp == null) {
qExp = new ArrayList<QExp>();
}
return this.qExp;
}
public void setQExp(List<QExp> qExp) {
this.qExp = qExp;
}
QExp entity :
------------------------
@Entity(name = "QExp")
public class QExp
implements Equals, HashCode, ToString
{........
protected List<Region> region;
@OneToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "MET_Q_EXP_REGION_J", joinColumns = {
@JoinColumn(name = "PARENT_QEXP_ID")
}, inverseJoinColumns = {@JoinColumn(name = "CHILD_REGION_ID") })
public List<Region> getRegion() {
if (region == null) {
region = new ArrayList<Region>();
}
return this.region;
}
public void setRegion(List<Region> region) {
this.region = region;
}
Region entity :
----------------
public class Region
implements Equals, HashCode, ToString
{..........
protected String pname;
...............
@Basic
@Column(name = "PNAME")
public String getPname() {
return pname;
}
public void setPname(String value) {
this.pname = value;
}
We have one form which corresponds to different fields from different entities.
Root is Study entity, which have OneToMany relation to QExp, QExp have OneToMany realtion with Region, all the mapping is fine.
If there is ONE Region, i.e. one field like Pname, I am able to persist it.
Issue comes up when we provide multiple form fields with same name for Region, (e.g. if there are 2 Pnames and both have value "#{regionHome.instance.pname}")
After clicking "Save" values for Region 2 (PName) field is only persisted, which somehow overrides Region 1 (PName) field, it should svae both.
We have a Entity "Region" and corresponding "regionhome", value for each region form field in xhtml is mapped to regionHome.instance.fieldname
study.xhtml & studyHome.java does all the processing, studyHome.persist method is called on clicking save from study.xhtml.
XHTML file :
-----------
<h:form id="studyForm">
<rich:panel>
<f:facet name="header">Region 1</f:facet>
<s:decorate id="pnameDecoration" template="layout/edit.xhtml">
<ui:define name="label">PName</ui:define>
<h:inputText id="name" required="true"
value="#{regionHome.instance.pname}"/>
</s:decorate>
</rich:panel>
<rich:panel>
<f:facet name="header">Region 2</f:facet>
<s:decorate id="pnameDecoration" template="layout/edit.xhtml">
<ui:define name="label">PName</ui:define>
<h:inputText id="name" required="true"
value="#{regionHome.instance.pname}"/>
</s:decorate>
</rich:panel>
<h:commandButton id="save" value="Save" action="#{studyHome.persist}" rendered="#{!studyHome.managed}"/>
</h:form>
studyHome :
-----------
@Name("studyHome")
public class StudyHome extends EntityHome<Study>
{............
-----------
public String persist(){
List<QExp> qList = new ArrayList<QExp>();
qList.add(getQExp());
initStudy().setQExp(qList);
return super.persist();
}
public QExp getQExp() {
QExp quantExp = qExpHome.initQExp();
quantExp.setRegion(getRegion());
return quantExp;
}
public List<Region> getRegion() {
List<Region> regionList=new ArrayList<Region>();
Region region = regionHome.initRegion();
region.setTreatment(getTreatment());
regionList.add(region);
return regionList;
}
Study entity :
--------------
@Entity(name = "Study")
public class Study
implements Equals, HashCode, ToString
{......
protected List<QExp> qExp;
@JoinTable(.....)
public List<QExp> getQExp() {
if (qExp == null) {
qExp = new ArrayList<QExp>();
}
return this.qExp;
}
public void setQExp(List<QExp> qExp) {
this.qExp = qExp;
}
QExp entity :
------------------------
@Entity(name = "QExp")
public class QExp
implements Equals, HashCode, ToString
{........
protected List<Region> region;
@OneToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "MET_Q_EXP_REGION_J", joinColumns = {
@JoinColumn(name = "PARENT_QEXP_ID")
}, inverseJoinColumns = {@JoinColumn(name = "CHILD_REGION_ID") })
public List<Region> getRegion() {
if (region == null) {
region = new ArrayList<Region>();
}
return this.region;
}
public void setRegion(List<Region> region) {
this.region = region;
}
Region entity :
----------------
public class Region
implements Equals, HashCode, ToString
{..........
protected String pname;
...............
@Basic
@Column(name = "PNAME")
public String getPname() {
return pname;
}
public void setPname(String value) {
this.pname = value;
}