I am trying to learn JSF, EJB and eclipselink, JPA using the following example:
an index.xhtml page displays a list of employees from an Employee Entity:
Entity:
@Entity
@Table(name="EMPLOYEE")
@NamedQuery(name="Employee.findAll", query="SELECT s FROM Employee s")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String empcode;
private String empdesc;
SessionBean function:
public List<Employee> getEmployeeFindAll() {
return em.createNamedQuery("Employee.findAll").getResultList();
}
index.xhtm:
<body>
<h:form>
<h:dataTable border="1" var="temp" id="d1"
value="#{EmployeeManagedBean.EmployeeFindAll}">
<f:facet name="header">
<h:outputText id="o1" value="EmpTable"></h:outputText>
</f:facet>
<h:column id="c1">
<f:facet name="header">
<h:outputText id="o2" value="#{temp.empcode}"></h:outputText>
</f:facet>
</h:column>
<h:column id="c2">
<f:facet name="header">
<h:outputText id="o3" value=""#{temp.empdesc}""> /h:outputText>
</f:facet>
</h:column>
</h:dataTable>
</h:form>
So far so good, index.xhtml
works fine. Now I have a second entity EmployeeQualification
:
@Entity
@Table(name="EMPQUA")
@NamedQuery(name="Empqua.findAll", query="SELECT n FROM Empqua n")
public class Empqua implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private EmpquaPK id;
@Temporal(TemporalType.DATE)
private Date since;
where @EmbeddedId
is :
@Embeddable
public class EmpquaPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="EMPCODE", length = 8)
private String empcode;
@Column(name="QUACODE")
private String quacode;
What I want to do is use the index.xhtml
datatable contents as links to a second page e.g empqua.xhtml
where I will display a data table which will contain all Empqua records for the selected Empcode.
So what I think I need is a function in my EmpquaManagedbean
which will take empcode
as input, call the corresponding function in my EmpquaSessionbean and return a list of Empqua objects, containing all records of Empqua for the given empcode:
EmpquaManagedbean:
public List<Empqua> getEmpquaforSelectedEmpcode(String empcode){
return getSessionFacade().getEmpquaByEmpcode(empcode);
}
Empqua Session Bean:
public List<Empqua> getEmpquaByEmpcode(String empcode){
return em.createQuery("SELECT a FROM Empqua a where (a.id.empcode) = :empcode").setParameter("empcode", empcode).getResultList();
}
Now my problems begin. I need to pass the parameter empcode
from my index.xhtml
datatable (when the link is clicked) to the managed bean and return a page empqua.xhtml
. The first problem is, how do I pass the parameter?
<h:commandLink value="#{temp.empcode}" action="#{EmpQuaManagedBean.getEmpquaforSelectedEmpcode(temp.empcode)}"></h:commandLink>
returns a method not found exception. Furthermore I probably need to call this through another function which will return String empqua.xhtml
to move to the next page.