DataTable -LazyLoading- LazyDataModel problem

UI Components for JSF
Post Reply
Sunil
Posts: 47
Joined: 23 Jun 2011, 09:15

29 Jul 2011, 08:28

Hi All,

I'm trying lazy loading for my application.
And I'm doing in such a way:

Code: Select all

public class BreederController implements Serializable {
  private Breeder breeder;
 private LazyDataModel<Breeder> breederLazy;

 @PostConstruct
    public void createDataModel() {
         breederLazy = new LazyDataModel<Breeder>() {
            

            @Override
            public List<Breeder> load(int start, int size, String order, boolean bdesc, Map<String, String> filter) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        };
       
    }

    public Breeder getBreeder() {
        return breeder;
    }

    public void setBreeder(Breeder breeder) {
        this.breeder = breeder;
    }
    
    public LazyDataModel<Breeder> getBreederLazy() {
        return breederLazy;
    }
And my ui:

Code: Select all

<p:dataTable id="table1" var="item" 
                                 value="#{breederController.breederLazy}" 
                                 paginator="true" rows="5" 
                                 lazy="true" 
                                 styleClass="Table"
                                 selection="#{breederController.breeder}" 
                                 selectionMode="single"  
                                 onRowSelectComplete="edited.show()" 
                                 onRowSelectUpdate="display">
But the result is: No records found in table.
can anyone help me in this regard.

Thanks in advance

Sunil

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

29 Jul 2011, 10:02

You don't load any records from your database. In your load-method you only throw an exception. You have to load your data from the database there.
If you use DAOs this could look something like this:

Code: Select all

@Override
            public List<Breeder> load(int start, int size, String order, boolean bdesc, Map<String, String> filter) {
                myDAO.get(start,size,order,bdesc,filter);
            }
If you use Hibernate/JPA maybe this will help you: http://javaeeinsights.wordpress.com/201 ... teria-api/

Sunil
Posts: 47
Joined: 23 Jun 2011, 09:15

29 Jul 2011, 10:14

Thanks for your reply!
can you help me in doing lazy loading in the following code:

Code: Select all

public class BreederController  implements Serializable
{
private List<Breeder> breederCollection;
private Breeder breeder;
private LazyDataModel<Breeder> breederLazy;
@EJB
private BreederFacade ejbFacade;

public BreederController()
    {
    }
    /** Creates a new instance of BreederController */

   public List<Breeder> getBreederCollection ()
   {
       try
        {
            this.breederCollection = ejbFacade.getBreederCollection();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return breederCollection;
    }

public LazyDataModel<Breeder> getBreederLazy() {
        return breederLazy;
    }

    @PostConstruct
    public void createDataModel() {
        LazyDataModel m = new LazyDataModel<Breeder>() {
            public List<Breeder> load(int start, int size, String order, boolean desc, Map<String, String> filter) {
                logger.log(Level.INFO, "Loading the lazy breeder data between {0} and {1}", new Object[]{start, (start+size)});  
                return ejbFacade.load(start, size, order, desc, filter);
            }


            /*public List<Breeder> fetchLazyData(int i, int i1) {
                throw new UnsupportedOperationException("Not supported yet.");
            }*/
        };
        breederLazy = m;

    }

   public Breeder getBreeder(){
       return breeder;
   }

    public void setBreeder(Breeder breeder) {
        this.breeder = breeder;
    }

and the ui:

Code: Select all

<p:dataTable id="table1" var="item" 
                                 value="#{breederController.breederCollection}" 
                                 paginator="true" rows="5" 
                                 dynamic="true" 
                                  styleClass="Table"
                                 selection="#{breederController.breederID}" 
                                 selectionMode="single"  
                                 onRowSelectComplete="edited.show()" 
                                 onRowSelectUpdate="display">
Awaiting your reply!

Sunil

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

29 Jul 2011, 12:58

What exactly is not working? Are you getting any errors?

Additionally you have to call "breederLazy.setRowCount(int rowCount)" so the dataTable "knows" how many table-entries are existing overall.
If you have a look at the showcase [1], you can see how this works. But you have to make sure that the rowCount is set everytime a new entry is added or entries are deleted, so the dataTable-component can react to the change.

You can also try to edit your code as follows:

Code: Select all

public List<Breeder> load(int start, int size, String order, boolean desc, Map<String, String> filter) {
                List<Breeder> result = ejbFacade.load(start, size, order, desc, filter);
                return result;
            }
And then you can debug and see if the list "result" contains data or not.

[1] http://www.primefaces.org/showcase-labs ... leLazy.jsf

Sunil
Posts: 47
Joined: 23 Jun 2011, 09:15

29 Jul 2011, 13:21

Million Thanks Robert!
Finally it works :D.
Failed to create an object with list.
There was a data in Result.

Thanks
Sunil

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 50 guests