hi this is my first tutorial in this tutorial im going to explain how to populate primefaces data tables .there are zillions of tutorials in net about data table but most of them populate it with data from list so what makes this post different from others
1)every time we run the web server prime faces should load data tables with values got from hibernate for that every time we run the program Hibernate should execute
code:
Session sn=Util.getSessionFactory().openSession();
sn.beginTransaction();
Query qry=sn.createQuery("from Solodemo");
ArrayList<Solodemo> list= new ArrayList<Solodemo>(qry.list());
Explanation:
so every time we run web server this " Query qry=sn.createQuery("from Solodemo");" has to be executed
we use "qry.list()" to convert the query result in to list
and here "ArrayList<Solodemo> list= new ArrayList<Solodemo>(qry.list());"
and converting the list in to arry list and calling them in the xhtml page would be enough lets see the full code.
package hibernate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="solodemo")
public class Solodemo {
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public Solodemo(String fname, String lanme) {
this.fname = fname;
this.lanme = lanme;
}
public String getLanme() {
return lanme;
}
public void setLanme(String lanme) {
this.lanme = lanme;
}
Solodemo(){}
@Column(name="fname")
@Id
private String fname;
@Column(name="lname")
private String lanme;
}
this is used to create a table in hibernate
and the Managed Bean class is
Managed Bean: bean that is controlled by JSF
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>DATA TABLES DEMO</title>
</h:head>
<h:body>
<h:form>
<h1>output values</h1>
<p:dataTable value="#{data.init()}" var="so">
<p:column headerText="firstname">
<h:outputText value="#{so.fname}"/>
</p:column>
<p:column headerText="lanme">
<h:outputText value="#{so.lanme}"/>
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
here in the value attribute i have used #{data.init()}because it will execute the query and returns the list evey time we runs the webserver
output:
thanks you for reading it ,fell free to comment and ask for any doubt
so far what we have is a plain data table today we are going to modify the data table by adding pagination and lazy loading
pagination an lazy loading are extremely useful when we want to load large number of records
Pagination:
pagination specifies the number of records the data table wants to dispaly per page if there is 100 records we specify 10 records per page then data table will display only only 10 records in the first page .remaining records will be displayed in the subsequent pages
LAZyloading
the short come with pagination is that it loads all the records or rows in to the memory on request image if the query returns millions of records .at this point lazy loading comes to our rescue when we specify lazy=true.it only loads only number of records specified per request
for eg) if number of records is specified as 10 it will load only 10 records from the memory .upon next request it will load next ten records
lets get in to the example
first let me add pagination to the data table hibernate code is same expect that i added some records
<p:dataTable value="#{data.init()}" var="so" paginator="true" rows="10" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="10,12,15">
if we look above code we have add two important things paginator="true" rows="10"
next lets implement lazy loading
1)every time we run the web server prime faces should load data tables with values got from hibernate for that every time we run the program Hibernate should execute
code:
Session sn=Util.getSessionFactory().openSession();
sn.beginTransaction();
Query qry=sn.createQuery("from Solodemo");
ArrayList<Solodemo> list= new ArrayList<Solodemo>(qry.list());
Explanation:
so every time we run web server this " Query qry=sn.createQuery("from Solodemo");" has to be executed
we use "qry.list()" to convert the query result in to list
and here "ArrayList<Solodemo> list= new ArrayList<Solodemo>(qry.list());"
and converting the list in to arry list and calling them in the xhtml page would be enough lets see the full code.
package hibernate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="solodemo")
public class Solodemo {
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public Solodemo(String fname, String lanme) {
this.fname = fname;
this.lanme = lanme;
}
public String getLanme() {
return lanme;
}
public void setLanme(String lanme) {
this.lanme = lanme;
}
Solodemo(){}
@Column(name="fname")
@Id
private String fname;
@Column(name="lname")
private String lanme;
}
this is used to create a table in hibernate
and the Managed Bean class is
Managed Bean: bean that is controlled by JSF
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>DATA TABLES DEMO</title>
</h:head>
<h:body>
<h:form>
<h1>output values</h1>
<p:dataTable value="#{data.init()}" var="so">
<p:column headerText="firstname">
<h:outputText value="#{so.fname}"/>
</p:column>
<p:column headerText="lanme">
<h:outputText value="#{so.lanme}"/>
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
here in the value attribute i have used #{data.init()}because it will execute the query and returns the list evey time we runs the webserver
output:
output values
thanks you for reading it ,fell free to comment and ask for any doubt
so far what we have is a plain data table today we are going to modify the data table by adding pagination and lazy loading
pagination an lazy loading are extremely useful when we want to load large number of records
Pagination:
pagination specifies the number of records the data table wants to dispaly per page if there is 100 records we specify 10 records per page then data table will display only only 10 records in the first page .remaining records will be displayed in the subsequent pages
LAZyloading
the short come with pagination is that it loads all the records or rows in to the memory on request image if the query returns millions of records .at this point lazy loading comes to our rescue when we specify lazy=true.it only loads only number of records specified per request
for eg) if number of records is specified as 10 it will load only 10 records from the memory .upon next request it will load next ten records
lets get in to the example
first let me add pagination to the data table hibernate code is same expect that i added some records
<p:dataTable value="#{data.init()}" var="so" paginator="true" rows="10" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="10,12,15">
if we look above code we have add two important things paginator="true" rows="10"
next lets implement lazy loading
No comments:
Post a Comment