I am using Spring 3, Hibernate 4 and JSF 2.0 and am trying to do primefaces autocomplete.
I have the following in DAO class to get values from Oracle function
@Override
public List<EmployeeDetail> getEmployeeDetails(String employeeNumber) {
List query = (List)entityManager.createNamedQuery("getEmp")
.setParameter("empNumber", employeeNumber)
.getSingleResult();
return query;
}
Converter class
public EmployeeNameConverter(
List<EmployeeDetail> employeeDB, EmployeeDetailService instance,
String employeeNumber) {
if (employeeDetailService == null) {
employeeDetailService = instance;
}
if (param == null) {
param = employeeNumber;
}
this.employeeDB = employeeDB;
}
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1,
String submittedValue) {
if (submittedValue.trim().equals("")) {
return null;
} else {
try {
// int number = Integer.parseInt(submittedValue);
employeeDB = getEmployeeDetailService().getEmployeeDetails(param);
for (EmployeeDetail emp : employeeDB) {
if (emp.getEmployeeNumber() == submittedValue) {
return emp;
}
}
} catch (NumberFormatException exception) {
throw new ConverterException(new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Conversion Error",
"Not a valid employee"));
}
}
In my Managedbean
I have complete method where I am passing employeeDetailService to my converter class.
public List<EmployeeDetail> complete(String query) {
List<EmployeeDetail> suggestions;
suggestions = new ArrayList<EmployeeDetail>();
try {
employee = (List<EmployeeDetail>) new EmployeeNameConverter(
employeeList, employeeDetailService, query);
for (EmployeeDetail p : employee) {
if (p.getEmployeeNumber().startsWith(query))
suggestions.add(p);
}
} catch (Exception e) {
System.out.println("exc " + e.getMessage());
e.printStackTrace();
}
return suggestions;
}
JSF Code
<p:autoComplete value="#{empMB.selectedEmployee}"
id="basicPojo" minQueryLength="6"
completeMethod="#{empMB.complete}" var="p"
itemLabel="#{p.employeeNumber}"
itemValue="#{p}" converter="#{p.employee}"
forceSelection="true" />
When I type in characters I am getting exception
EmployeeNameConverter cannot be cast to java.util.List
java.lang.ClassCastException: net.test.util.EmployeeNameConverter cannot be cast to java.util.List
How can I resolve this? Is this the correct approach or could someone kindly suggest better approach in achieving the same?
Update 1
@Override
public List<EmployeeDetail> getEmployeeDetails(String employeeNumber) {
List query = (List)entityManager.createNamedQuery("getEmp")
.setParameter("empNumber", employeeNumber)
.getSingleResult();
return query;
}
Exception
net.test.entity.EmployeeDetail cannot be cast to java.util.List
java.lang.ClassCastException: net.test.entity.EmployeeDetail cannot be cast to java.util.List
at net.test.dao.EmployeeDetailDAOImpl.getEmployeeDetails(EmployeeDetailDAOImpl.java:36)
and exception line is
List query = (List)entityManager.createNamedQuery("getEmp")
.setParameter("empNumber", employeeNumber)
.getSingleResult();
There are two main issues apparent here
You’re attempting to instantiate and manage a JSF converter instance by hand. Don’t do this. The converter is a construct designed for use by the JSF context only. It’s not the job of you the developer to call
newor supply constructor arguments to the converter class. Implement a simple converter per this example and configure on the<p:autoComplete/>From your comments, the reason you’ve taken to managing the converter by hand is to be able to access your DAO layer. As a workaround, you can add
@ManagedBeanto your converter and JSF will treat is as a managed bean and a converter. Being a managed bean, you’ll now be able to inject your DAO resource into it. This is not best practice, but only a workaround. This limitation on converters will be removed in JSF2.2 though :).While manipulating the converter, you’re attempting to treat it like a regular POJO and trying to cast it into several incompatible types as a result