Solved: LazyDataModel problem

UI Components for JSF
Post Reply
zipito
Posts: 10
Joined: 25 Aug 2009, 14:51

14 Nov 2009, 16:04

Good day,

I'm trying to create the LazyDataModel for my Seam-based application.

I'm doing this in such way:

Code: Select all

public class PagedDataModel<E extends UniqaDocument> extends LazyDataModel<E> {
	
	private EntityQuery<E> entityQuery;

	@Override
	public List<E> fetchLazyData(int first, int pageSize) {
		entityQuery.setFirstResult(first);
		entityQuery.setMaxResults(pageSize);
		return entityQuery.getResultList();
	}

	@Override
	public int getRowCount() {
		return this.entityQuery.getResultCount().intValue();
	}
	
	public void setEntityQuery(EntityQuery<E> entityQuery) {
		this.entityQuery = entityQuery;
	}	

}
And use this class like following:

Code: Select all

@Name("sprFizLicList")
public class SprFizLicList extends PagedDataModel<SprFizLic>{

	@In(value="#{sprFizLicEntityQuery}", create=true)
	SprFizLicEntityQuery sprFizLicEntityQuery;
	
	@Create
	public void setSprFizLicEntityQuery() {
		super.setEntityQuery(sprFizLicEntityQuery);
	}
}
with ui:

Code: Select all

		<p:dataTable var="tt" value="#{sprFizLicList}"  lazy="true" dynamic="true" rows="10">
			<p:column>
				<h:outputText value="#{tt.description}" />
			</p:column>
			
			<p:column>
				<h:outputText value="#{tt.denRojd}" >
					<f:convertDateTime pattern="dd.MM.yyyy"/>
				</h:outputText>
			</p:column>
		</p:dataTable>
but always receive exception:

java.lang.ClassCastException: javax.faces.model.ListDataModel cannot be cast to org.primefaces.model.LazyDataModel
at org.primefaces.component.datatable.DataTable.loadLazyData(DataTable.java:436)
at org.primefaces.component.datatable.DataTableRenderer.encodeRows(DataTableRenderer.java:384)
at org.primefaces.component.datatable.DataTableRenderer.encodeTable(DataTableRenderer.java:339)
at org.primefaces.component.datatable.DataTableRenderer.encodePartially(DataTableRenderer.java:94)
at org.primefaces.component.datatable.DataTable.encodePartially(DataTable.java:460)
at org.primefaces.application.PrimeFacesPhaseListener$2.invokeContextCallback(PrimeFacesPhaseListener.java:169)
at javax.faces.component.UIData.invokeOnComponent(UIData.java:808)
at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:720)
at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:675)
at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:720)
at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:675)
at org.primefaces.application.PrimeFacesPhaseListener.beforePhase(PrimeFacesPhaseListener.java:91)
at com.sun.faces.lifecycle.Phase.handleBeforePhase(Phase.java:214)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:96)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
Can someone help me with this problem?
Last edited by zipito on 14 Nov 2009, 23:18, edited 1 time in total.

zipito
Posts: 10
Joined: 25 Aug 2009, 14:51

14 Nov 2009, 23:08

I've managed it to work like following:

Code: Select all


public class LazyModelEntityQuery<E> extends EntityQuery<E>{

	public LazyModelEntityQuery() {
		super();
		constructLazyModel();
	}
	
	private LazyDataModel<E> lazyModel;
	
	protected void constructLazyModel() {
		final EntityQuery<E> instance = this;
		
		lazyModel = new LazyDataModel<E>(){

			@Override
			public List<E> fetchLazyData(int first, int pageSize) {
				instance.setFirstResult(first);
				instance.setMaxResults(pageSize);
				return instance.getResultList();
			}
			
			@Override
			public int getRowCount() {
				return instance.getResultCount().intValue();
			}
			
		};
	}
	
	public LazyDataModel<E> getLazyModel() {
		return lazyModel;
	}
}
but what a hell? How is that different from original one! (first post) Why do I need to wrap it as a variable inside of the component?

zipito
Posts: 10
Joined: 25 Aug 2009, 14:51

14 Nov 2009, 23:15

disregard the whole monologue :lol: everything works like in the first one... I don't understand the problem (I guess additional maven clean; maven compile; solved the problem. So the first solution works fine...

User avatar
bumble.bee
Posts: 723
Joined: 29 Sep 2010, 21:39
Location: United States

27 Jun 2011, 19:34

This is an old post, but I believe I figured out what is causing this issue. I don't think the problem was your imagination. It appears that you'll get the exception:
java.lang.ClassCastException: javax.faces.model.ListDataModel cannot be cast to org.primefaces.model.LazyDataModel
If the bean that holds the LazyDataModel is RequestScoped. Changing the scope to SessionScoped for example removes the Exception!

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 69 guests