gpt4 book ai didi

java - 无法在延迟加载数据表中使用数字筛选器 : ClassCastException String to Integer

转载 作者:行者123 更新时间:2023-12-01 04:11:34 25 4
gpt4 key购买 nike

这是我的代码:

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;
}

一切工作正常,除非我尝试使用“年份”列过滤器。如果它是一个字符串,它会工作,但是,“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:column 过滤器...

我应该做什么?

谢谢!!

最佳答案

异常跟踪分析表明,以下行导致了问题

List<Thing> things = (List<Thing>) criteria.list();

当您收到 ClassCastException 时,意味着 criteria.list() 返回的结果无法正确转换为 List。正如你所说,它适用于 String 文件,但不适用于 int,我可以在 Thing 类的 Hibernate 映射文件中看到问题。

当您将 year datatye 更改为 int 时,您需要在 DB 和 Hibernate 映射文件中进行相同的更改。由于缺少此内容,数据库结果即 Varchar 类型(String)无法转换为 int。因此引发了 ClassCastException。

因此,我的建议是更改数据库和 Hibernate 映射文件中的数据类型。这应该可以解决您的问题。

关于java - 无法在延迟加载数据表中使用数字筛选器 : ClassCastException String to Integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19879626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com