Spring 3、Hibernate 4、および JSF 2.0 を使用しており、primefaces のオートコンプリートを実行しようとしています。
Oracle関数から値を取得するために、DAOクラスに次のものがあります
@Override
public List<EmployeeDetail> getEmployeeDetails(String employeeNumber) {
List query = (List)entityManager.createNamedQuery("getEmp")
.setParameter("empNumber", employeeNumber)
.getSingleResult();
return query;
}
コンバータークラス
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"));
}
}
私のマネージドビーンで
employeeDetailService をコンバーター クラスに渡す完全なメソッドがあります。
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 コード
<p:autoComplete value="#{empMB.selectedEmployee}"
id="basicPojo" minQueryLength="6"
completeMethod="#{empMB.complete}" var="p"
itemLabel="#{p.employeeNumber}"
itemValue="#{p}" converter="#{p.employee}"
forceSelection="true" />
文字を入力すると例外が発生します
EmployeeNameConverter cannot be cast to java.util.List
java.lang.ClassCastException: net.test.util.EmployeeNameConverter cannot be cast to java.util.List
どうすればこれを解決できますか? これは正しいアプローチですか、それとも同じことを達成するためのより良いアプローチを親切に提案できますか?
更新 1
@Override
public List<EmployeeDetail> getEmployeeDetails(String employeeNumber) {
List query = (List)entityManager.createNamedQuery("getEmp")
.setParameter("empNumber", employeeNumber)
.getSingleResult();
return query;
}
例外
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)
そして例外行は
List query = (List)entityManager.createNamedQuery("getEmp")
.setParameter("empNumber", employeeNumber)
.getSingleResult();