Friday, 26 December 2014

Cloning in java(deep,shallow)

what is cloning in java ,cloning is making a exact copy of an object. in java cloning is done by field by field of primitive data types and return reference to objects

to clone a class we have  to implement Cloneable interface
override the method public object clone

package blogging;

public class Shallowcloning implements Cloneable {
   
    private int age;
     private int salary;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public Shallowcloning() {
    }
   
    public Object clone() throws CloneNotSupportedException{
        return super.clone();
       
    }

}
package blogging;

public class Clonningimplementation {

    public static void main(String[] args) {
       
       
    try{
    Shallowcloning sclone=new Shallowcloning();
    sclone.setSalary(200);
    sclone.setAge(28);// setting value for the objects
    //creting a new clone for object sclone
   
        Shallowcloning sclone2=(Shallowcloning)sclone.clone();
   
       


   
    //checking the values of clone 1 and clone 2
   
    System.out.println("Sclone  salary:"+sclone.getSalary());
    System.out.println("Sclone1 Age:"+sclone.getAge());
    System.out.println("Sclone2 salary:"+sclone2.getAge());
    System.out.println("Sclone2 Age:"+sclone2.getSalary());
   
   
   

    }

    catch(CloneNotSupportedException e){
        System.out.println("some problem occours with clonning ");
       
    }
   
   
   
}
}
   
        output

Sclone  salary:200
Sclone1 Age:28
Sclone2 salary:28
Sclone2 Age:200

what happened here is a field by field copy of object 1 ,the point to be noted here is object 2 is independent of object 1 ,so any changes done in object 2 will not affect object 1(only if primitive data types are present if reference data types are present we have to go for deep cloning )


sclone2.setAge(67);
    sclone2.setSalary(78000);//changing clone objects methods
    //after changing

Sclone2 salary:200
Sclone  salary:200
Sclone1 Age:28
Sclone2 age:67
Sclone2 salary:78000
 see the changes made to object 2 i.e the cloned object didn't affect the original object

till now we have seen shallow cloning ,next we shall see what is deep cloning

Deep cloning
so far we have cloned only primitive data types what if the object contains refrences for eg

 package blogging;

public class Deepcloning implements Cloneable
{
private Shallowcloning shallowcopy;
public Deepcloning() {
}
public Shallowcloning getShallowcopy() {
    return shallowcopy;
}
public void setShallowcopy(Shallowcloning shallowcopy) {
    this.shallowcopy = shallowcopy;
}
public String getEmployee() {
    return employee;
}
public void setEmployee(String employee) {
    this.employee = employee;
}
private String employee;

public Object clone() throws CloneNotSupportedException{
  
    return super.clone();}
  
  

}


here the class  Deepcloning has a reference


 public class Deepcloning implements Cloneable
{
public Shallowcloning shallowcopy;
public Deepcloning() {
}
public Shallowcloning getShallowcopy() {
    return shallowcopy;
}
public void setShallowcopy(Shallowcloning shallowcopy) {
    this.shallowcopy = shallowcopy;
}
public String getEmployee() {
    return employee;
}
public void setEmployee(String employee) {
    this.employee = employee;
}
private String employee;

public Object clone() throws CloneNotSupportedException{
  
    Deepcloning shls=(Deepcloning)super.clone();
    shls.setShallowcopy((Shallowcloning)shallowcopy.clone());
        return shls;
}
 }
and the implementation method

Deepcloning dp1=new Deepcloning();
    dp1.setEmployee("solomon");
    dp1.setShallowcopy(new Shallowcloning());
    dp1.getShallowcopy().setAge(11);
    dp1.getShallowcopy().setSalary(3000);
   
   
    System.out.println("orignal copy employee"+dp1.getEmployee());
   
    System.out.println("orignal copy employee"+dp1.getShallowcopy().getAge());
    System.out.println("orignal copy employee"+dp1.getShallowcopy().getSalary());
    Deepcloning dp2=(Deepcloning)dp1.clone();
    System.out.println("clone copy employee"+dp2.getEmployee());
    System.out.println("clone copy employee"+dp2.getShallowcopy().getAge());
    System.out.println("clone copy employee"+dp2.getShallowcopy().getSalary());
    dp1.getShallowcopy().setAge(25);
    System.out.println("original value"+dp1.getShallowcopy().getAge());
    System.out.println("clonned value "+dp2.getShallowcopy().getAge());


Friday, 12 December 2014

PRIMEFACES+JPA/HIBERENATE INTEGRATION (lazy loading,sorting,selection ,filtering)

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:


output values

firstnamelanme
aadumaadu
psychoganesh
seetusays
solosaysit

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