Pages

Wednesday, 11 September 2013

Exception - tricky interview questions

1) find the o/p of the following


package com.kb.client;

public class Exception1 {

 
 public static void main(String[] args) {
  String value = getValue();
  System.out.println(value);

 }

 private static String getValue() {
  
  try {
   System.out.println("before return from try");
   return "from try";
   
  } catch (Exception e) {
   System.out.println("before return from catch");
   return "from catch";
  }
  finally{
   System.out.println("before return from finally");
   return "from finally";
  }
 }

}
 
here when the control enters the try block it first prints the first line 
before return from trythen it has return statement , but finally should execute before try completes so control goes to finally, it prints finally block's first statement
 and it finds the return statement there so it returns from finally.
so output would be
 
before return from try
before return from finally
from finally
 
 
now i will put exception statement in try block as below
 
package com.kb.client;

public class Exception1 {

 
 public static void main(String[] args) {
  String value = getValue();
  System.out.println(value);

 }

 private static String getValue() {
  
  try {
   int i = 10/0;
   System.out.println("before return from try");
   return "from try";
   
  } catch (Exception e) {
   System.out.println("before return from catch");
   return "from catch";
  }
  finally{
   System.out.println("before return from finally");
   return "from finally";
  }
 }

}
 
in this case find the o/p ?

as explained above but try gets with the exception so catch starts execution and executes finally before catch returns so 
the o/p will be as below 

before return from catch
before return from finally
from finally

now if we comment return from finally for the above program as below

package com.kb.client;

public class Exception1 {

 
 public static void main(String[] args) {
  String value = getValue();
  System.out.println(value);

 }

 private static String getValue() {
  
  try {
   int i = 10/0;
   System.out.println("before return from try");
   return "from try";
   
  } catch (Exception e) {
   System.out.println("before return from catch");
   return "from catch";
  }
  finally{
   System.out.println("before return from finally");
   //return "from finally";
  }
 }

}
 
now in this case try as soon as get exception control goes to catch and catch executes first line and before executing return it goes to finally and executes first line .
since return in finally is commented it goes back to catch and executes return from catch.
so o/p would be as below

before return from catch
before return from finally
from catch


 

Sunday, 2 June 2013

 One To One Mapping in Hibernate

Using hibernate, if we want to put relationship between two entities means two Pojo classes, then in the database tables, there must exist foreign key relationship, we call it as Referential integrity.

there are many ways to do the mapping in Hibernate. lets see one of such way "one to one ".

we have 2 entities here 1)Author 2)AuthorContactDetails

we need to establish one to one relationship between these 2 entities now. means one author ms have one contact details .

in DB we will have 2 separate tables one for author and other one for author contact details.

in java also we will have 2 separate classes for each of the above tables.

then how can we give the associations between these 2 tables.  lets do it using hibernate annotation.

1)Author class
package com.kb.model;

import javax.persistence.Entity;

@Entity
public class Author {
 @Id
 @GeneratedValue
 private int authodNo;
 private String name;
 private String aboutAuthor;
 @OneToOne
 @JoinColumn(name="Author_Contact_Id")
 private AuthorContactDetails contactDetails;

 public int getAuthodNo() {
  return authodNo;
 }

 public void setAuthodNo(int authodNo) {
  this.authodNo = authodNo;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getAboutAuthor() {
  return aboutAuthor;
 }

 public void setAboutAuthor(String aboutAuthor) {
  this.aboutAuthor = aboutAuthor;
 }

 public void setContactDetails(AuthorContactDetails contactDetails) {
  this.contactDetails = contactDetails;
 }

 public AuthorContactDetails getContactDetails() {
  return contactDetails;
 }

}
2)AuthorContactDetails class
package com.kb.model;

import javax.persistence.Entity;

@Entity
public class AuthorContactDetails {
@Id
@GeneratedValue
private int id;
private String email;
private String mobile;
public int getId() {
 return id;
}
public void setId(int id) {
 this.id = id;
}
public String getEmail() {
 return email;
}
public void setEmail(String email) {
 this.email = email;
}
public String getMobile() {
 return mobile;
}
public void setMobile(String mobile) {
 this.mobile = mobile;
}
}

look at the Author class very closely, we can find the association between them.
we have used @onetoone annotation to indicate hibernate that this class has one dependent table in the DB.
and please do the mapping(take primary key of that contact table and put in the author table as a foreign key)
so now mapping is done between these 2 tables. whenever we persist the author object it expects contact details to be persisted in the DB.
so persist contact details  and author and leave the mapping for hibernate, then we can see one column which is used as a mapping between 2 tables.

see below program for the same




package com.kb.dao;

import org.hibernate.Session;

public class OneToOne {

 public static void main(String[] args) {
  SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  Session session = sessionFactory.openSession();
  Author author = new Author();
  author.setName("kb");
  author.setAboutAuthor("good one");
  
  AuthorContactDetails contactDetails = new AuthorContactDetails();
  contactDetails.setEmail("abc@abcd.com");
  contactDetails.setMobile("1234567891");
  
  author.setContactDetails(contactDetails);
  
  try {
   session.beginTransaction();
   session.save(author);
   session.save(contactDetails);
   session.getTransaction().commit();
  } catch (Exception e) {
  session.getTransaction().rollback();
  }
  finally{
   session.close();
  }
  
  session=sessionFactory.openSession();
  author = (Author)session.load(Author.class, 1);

  System.out.println("name of the author is "+author.getName());
 }

}
now run the above program we will get the below output

name of the author is kb

Saturday, 1 June 2013

Serialization and externalization in java

Serialization is the process of writing an object state into the file.
state of an object means atrributes and its values and in future if we need that object we can rebuild it from the file and  we call that process as "de serialization"
so to achive serialization in java,  any class must implement one marker interface.
that marker interface is "serializable".
lets understand this process through below example
package com.kb.model;

import java.io.Serializable;

public class Employee implements Serializable{

 private String name;
 private int age;
 private String designation;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getDesignation() {
  return designation;
 }
 public void setDesignation(String designation) {
  this.designation = designation;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
}

package com.kb.client;

import java.io.File;

public class Serialize {

 public static void main(String[] args) {
  Employee emp = new Employee();
  emp.setAge(25);
  emp.setDesignation("se");
  emp.setName("kb");
  File file = new File("employee.ser");
  FileOutputStream fout = null;
  ObjectOutputStream oout = null;
  try {
   fout = new FileOutputStream(file);
   oout = new ObjectOutputStream(fout);
   oout.writeObject(emp);
  } catch (IOException e) {
  } catch (Exception e) {

  }

  finally {
   if (oout != null) {
    try {
     oout.flush();
     oout.close();
     oout = null;
    } catch (IOException e) {
     e.printStackTrace();
    }
    if (fout != null) {
     try {
      fout.close();
      fout = null;
     } catch (IOException e) {
      e.printStackTrace();
     }

    }
   }
  }
 }

}


when we execute the above program, one file gets created in our project names as "employee.ser". this file contains the information about the employee object which we have written into this file.
this process is called serialization.

Now lets see de-serialization.
its just reading a serialized object from the file into the object back.
means retaining the object state.
lets do it as below
package com.kb.client;

import java.io.File;

public class DeSerialize {

 public static void main(String[] args) {
  File file = new File("employee.ser");

  FileInputStream fin = null;
  ObjectInputStream oin = null;

  try {
   fin = new FileInputStream(file);
   oin = new ObjectInputStream(fin);
   Employee emp = (Employee) oin.readObject();
   System.out.println(emp.getAge());
   System.out.println(emp.getDesignation());
   System.out.println(emp.getName());
  } catch (Exception e) {
   // TODO: handle exception
  }

  finally {
   if (oin != null) {
    try {
     oin.close();
     oin = null;
    } catch (IOException e) {
     e.printStackTrace();
    }
    ;

   }

   if (fin != null) {
    try {
     fin.close();
     fin = null;
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  }

 }

}

now run the above program and see the below output


25
se
kb

Remember in serialization
1)If we dont want to save any of the attribute from the serializable class then we can make such attributes as "transient".
2)all static fields can not be serializable sine its a class attribute not a object attribute.

in the above example if we dont want to save attribute designation then make it as transient as below
transient String designation;
then run both the program, in the output we can see "null " for that designation in the de-serializable program output.

now what if the employee class has address class reference and if address class is not serialized then while serializing employee object we will get an exception

scenario explained above through program is as below
address class - not implemented serializable interface.

package com.kb.model;

public class Address {
 private String street;
 private String location;
 public String getStreet() {
  return street;
 }
 public void setStreet(String street) {
  this.street = street;
 }
 public String getLocation() {
  return location;
 }
 public void setLocation(String location) {
  this.location = location;
 }

}
now consider below employee class
package com.kb.model;

import java.io.Serializable;

public class Employee implements Serializable{

 private String name;
 private int age;
 private transient String designation;
 private Address address;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getDesignation() {
  return designation;
 }
 public void setDesignation(String designation) {
  this.designation = designation;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public Address getAddress() {
  return address;
 }
 public void setAddress(Address address) {
  this.address = address;
 }
}
now see the below client program to serialize
package com.kb.client;

import java.io.File;

public class Serialize {

 public static void main(String[] args) {
  
  
  Employee emp = new Employee();
  emp.setAge(25);
  emp.setDesignation("se");
  emp.setName("kb");
  Address address = new Address();
  address.setLocation("bang");
  address.setStreet("rajajinagar");
  emp.setAddress(address);
  File file = new File("employee.ser");
  FileOutputStream fout = null;
  ObjectOutputStream oout = null;
  try {
   fout = new FileOutputStream(file);
   oout = new ObjectOutputStream(fout);
   oout.writeObject(emp);
   System.out.println("done");
  } catch (IOException e) {
  } catch (Exception e) {

  }

  finally {
   if (oout != null) {
    try {
     oout.flush();
     oout.close();
     oout = null;
    } catch (IOException e) {
     e.printStackTrace();
    }
    if (fout != null) {
     try {
      fout.close();
      fout = null;
     } catch (IOException e) {
      e.printStackTrace();
     }

    }
   }
  }
 }

}
run this , we will end up with an exception "not serializable exception"

so to serialize any object , if its clas is having any derived type reference like address then those derived types class must also be serialized first otherwise we will end with the above exception.

we will see about externalization and its flexibility and advantages over serialization in next post.

Thanks for reading

Thursday, 30 May 2013


Embeddable in hibernate

Person table
Id
Bigint
Name
Varchar
Address
Streetname
City
State
pincode
jobType
Varchar















Observe the above table structure , we can easily persist id,name,jobtype into person table.
But what about address attribute ?

Address is not having any basic datatype that java supports and db supports.

Address is another class  but it is composed inside person class.

So the way to save this address attribute inside person table – we need to tell hibernate that

Please go to the address class and find all basic attributes and put each of them in the person table.

So our solution looks something like this

Person table
Id
Bigint
Name
Varchar
Streetname
Varchar
City
Varchar
State
Varchar
pincode
Varchar
jobType
varchar


How to achive this in java using hibernate ?

Very simple – make the class address as embeddable .
And keep its reference in person class.

That’s it wow great.

Now when you persist person, your address class’s  each attribute will go as a separate column inside person table.

Observe carefully the below example.

Person class

package com.kb.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="PERSON_DETAILS")
public class Person {

      @Id
      @GeneratedValue
      private long id;
      private String name;
      private Address address;
      private String jobType;

      public long getId() {
            return id;
      }

      public void setId(long id) {
            this.id = id;
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

      public String getJobType() {
            return jobType;
      }

      public void setJobType(String jobType) {
            this.jobType = jobType;
      }

      public Address getAddress() {
            return address;
      }

      public void setAddress(Address address) {
            this.address = address;
      }
     
}


Address class

package com.kb.model;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Address {
                @Column(name="STREET_NAME")
                private String streetName;
                @Column(name="CITY")
                private String city;
                @Column(name="STATE")
                private String state;
                @Column(name="PINCODE")
                private String pinCode;
                public String getStreetName() {
                                return streetName;
                }
                public void setStreetName(String streetName) {
                                this.streetName = streetName;
                }
                public String getCity() {
                                return city;
                }
                public void setCity(String city) {
                                this.city = city;
                }
                public String getState() {
                                return state;
                }
                public void setState(String state) {
                                this.state = state;
                }
                public String getPinCode() {
                                return pinCode;
                }
                public void setPinCode(String pinCode) {
                                this.pinCode = pinCode;
                }

}

Client program

package com.kb.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.kb.model.Address;
import com.kb.model.Person;

public class EmbeddedClient1 {

               
                public static void main(String[] args) {
Person person = new Person();
Address address = new Address();
address.setStreetName("maruti nagar");
address.setCity("bangalore");
address.setState("karnataka");
address.setPinCode("567891");
person.setName("kbc");
person.setJobType("Software");
person.setAddress(address);

//build session factory and get session
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
try {
                session.beginTransaction();
                session.save(person);
                session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
}
finally{
                session.close();
}
                }

}


output in mysql




Now what if we have collection of address inside person class(like home address,office address,permanent address,temporary address)

Now  assume we put all the columns in person table then for same user all these addresses has to be in person table. During that time, it becomes more redundancy as we need to show each row with same user details for each different address which is not really good.

The problem will become like this

Person table
Id
Name
Jobtype
Streetname
City
State
pincode
1
User1
Software
Infantry road
Bangalore
Karnataka
123456
1
User1
Software
Mg road
Bangalore
Karnataka
654321

For the same user 2 addresses and because of that user information is redundant for each address information.

So if we have 100 such repetitive dependent object then 100 times user has to be duplicated in db.

So solution is separate the user and address as 2 tables and put userid in address table to know that this
address belongs to this particular user.

How can we achieve this in java using hibernate

Solution is very simple, just on top of collection element declaration in person table , put this annotation
@ElementCollection.

See below code for complete example.

Person class

package com.kb.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="PERSON_DETAILS")
public class Person {

      @Id
      @GeneratedValue
      private long id;
      private String name;
      @ElementCollection
      private List<Address> address = new ArrayList<Address>();
      private String jobType;

      public long getId() {
            return id;
      }

      public void setId(long id) {
            this.id = id;
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

      public String getJobType() {
            return jobType;
      }

      public void setJobType(String jobType) {
            this.jobType = jobType;
      }

      public List<Address> getAddress() {
            return address;
      }

      public void setAddress(List<Address> address) {
            this.address = address;
      }

     
     
}


Address class


package com.kb.model;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Address {
                @Column(name="STREET_NAME")
                private String streetName;
                @Column(name="CITY")
                private String city;
                @Column(name="STATE")
                private String state;
                @Column(name="PINCODE")
                private String pinCode;
                public String getStreetName() {
                                return streetName;
                }
                public void setStreetName(String streetName) {
                                this.streetName = streetName;
                }
                public String getCity() {
                                return city;
                }
                public void setCity(String city) {
                                this.city = city;
                }
                public String getState() {
                                return state;
                }
                public void setState(String state) {
                                this.state = state;
                }
                public String getPinCode() {
                                return pinCode;
                }
                public void setPinCode(String pinCode) {
                                this.pinCode = pinCode;
                }

}
Client program

package com.kb.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.kb.model.Address;
import com.kb.model.Person;

public class EmbeddedClient1 {

               
                public static void main(String[] args) {
Person person = new Person();
Address address = new Address();
address.setStreetName("maruti nagar");
address.setCity("bangalore");
address.setState("karnataka");
address.setPinCode("567891");
person.setName("kbc");
person.setJobType("Software");

Address address1 = new Address();
address1.setStreetName("maruti nagar1");
address1.setCity("bangalore1");
address1.setState("karnataka1");
address1.setPinCode("567891new");

person.getAddress().add(address);
person.getAddress().add(address1);

//build session factory and get session
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
try {
                session.beginTransaction();
                session.save(person);
                session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
}
finally{
                session.close();
}
                }

}


output in mysql





 Thanks for reading