这是我的代码:
XHTML:
<p:dataTable id="dataTable" var="variable" value="#{myBean.lazyList}"
widgetVar="widgetVar"
emptyMessage="Empty."
filteredValue="#{myBean.filteredLazyList}"
paginator="true" rows="100"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
rowsPerPageTemplate="100,150,200" lazy="true" rendered="true"
selectionMode="single" selection="#{myBean.selectedRow}" >
<p:column id="yearColumn" filterBy="#{variable.year}" sortBy="#{variable.year}" headerText="Ano" filterMatchMode="contains" >
<h:outputText value="#{variable.year}" />
</p:column>
从我的LazyDataModel实现中加载方法:
@Override
public List<Thing> load(int startingAt, int maxPerPage, String sortField, SortOrder sortOrder, Map<String, String> filters) {
MyDAO dao = new MyDAO();
try {
myList = dao.findMyThings(startingAt, maxPerPage, sortField, sortOrder, filters);
} catch (Exception e) {
e.printStackTrace();
}
if(getRowCount() <= 0){
setRowCount(dao.countMyThingsNoPagination(filters));
}
if(maxPerPage > 0){
setPageSize(maxPerPage);
}
return myList;
}
我的DAO课程中的搜索方法:
public List<Thing> findMyThings(int startingAt, int maxPerPage,
String sortField, SortOrder sortOrder, Map<String, String> filters) {
session = HibernateUtil.getSession();
Criteria criteria =session.createCriteria(Thing.class);
if(maxPerPage > 0){
criteria.setMaxResults(maxPerPage);
}
criteria.setFirstResult(startingAt);
for(Map.Entry<String, String> entry : filters.entrySet()){
if(!Util.isEmpty(entry.getValue())){
if(entry.getKey().contains(".")){
String first = (entry.getKey().split("\\."))[0];
criteria.createAlias(first, first);
}
criteria.add(Restrictions.and(Restrictions.eq(entry.getKey(), entry.getValue())));
}
}
if(sortField != null && !sortField.isEmpty()){
if(sortOrder.equals(SortOrder.ASCENDING)){
criteria.addOrder(Order.asc(sortField) );
}
else if(sortOrder.equals(SortOrder.DESCENDING)){
criteria.addOrder(Order.desc(sortField) );
}
}
else{
criteria.addOrder(Order.desc("insertDate") );
}
@SuppressWarnings("unchecked")
List<Thing> things = (List<Thing>) criteria.list();
session.close();
return things;
}
除非我尝试使用“年”列过滤器,否则一切正常。如果它是一个String,它会工作但是,“year”是一个int类型,我收到以下错误:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at org.hibernate.type.descriptor.java.IntegerTypeDescriptor.unwrap(IntegerTypeDescriptor.java:36)
at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$1.doBind(IntegerTypeDescriptor.java:57)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:93)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1968)
at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1939)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1874)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1835)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1815)
at org.hibernate.loader.Loader.doQuery(Loader.java:899)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
at org.hibernate.loader.Loader.doList(Loader.java:2522)
at org.hibernate.loader.Loader.doList(Loader.java:2508)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2338)
at org.hibernate.loader.Loader.list(Loader.java:2333)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:124)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1662)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
at dao.ThingDAO.findThings(ThingDAO.java:99)
at lazyDataModel.LazyThingDataModel.load(LazyThingDataModel.java:25)
at org.primefaces.component.datatable.DataTable.loadLazyData(DataTable.java:737)
at org.primefaces.component.datatable.feature.FilterFeature.encode(FilterFeature.java:85)
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:76)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:879)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1650)
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:479)
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:175)
at org.primefaces.component.api.UIData.visitTree(UIData.java:675)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1487)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1487)
at javax.faces.component.UIForm.visitTree(UIForm.java:331)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1487)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1487)
at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:361)
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:282)
at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:57)
at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:968)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1643)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:378)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:127)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:117)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:135)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:309)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:98)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
我想过可能会使用转换器来过滤器,但我不知道如何将转换器设置为p:列过滤器......
我该怎么办?
谢谢!
答案 0 :(得分:0)
Exception Trace的分析说,以下行会导致问题
List<Thing> things = (List<Thing>) criteria.list();
当您收到ClassCastException
时,表示criteria.list()
返回的结果无法正确投射到列表。正如你所说,它使用String工作文件而不是int工作,我可以在Thing
类的Hibernate映射文件中看到问题。
当您将year
datatye更改为int
时,您need to change same in DB & Hibernate Mapping file
。由于缺少这种情况,DB结果即Varchar类型(String)无法转换为int。因此引发了ClassCastException。
所以,我的建议是改变DB和Hibernate Mapping文件中的数据类型。这应该可以解决你的问题。