JDBC vs Hibernate



  1. JDBC:
    1. JDBC stands for Java Database Connectivity allows developers to connect, query and update a database using the Structured Query Language(SQL).
    2.  JDBC API standard provides Java developers to interact with different RDBMS and access table data through Java application without learning RDBMS details and using Database Specific JDBC Drivers.
  2. Hibernate:
    1. Hibernate is an Object-Relational Mapping (ORM) solution for java. It is a powerful, high performance object/relational persistence and query service.
    2. It allows us to develop persistent classes following object-oriented idiom – including association, inheritance and polymorphism.
Difference beetween JDBC and Hibernate.

JDBC 
Hibernate 
With JDBC, developer has to write code to map an object model's data representation to a relational data model and its corresponding database schema.  
Hibernate is flexible and powerful ORM solution to map Java classes to database tables. Hibernate itself takes care of this mapping using XML files so developer does not need to write code for this. 
With JDBC, the automatic mapping of Java objects with database tables and vice versa conversion is to be taken care of by the developer manually with lines of code.  
Hibernate provides transparent persistence and developer does not need to write code explicitly to map database tables tuples to application objects during interaction with RDBMS.  
JDBC supports only native Structured Query Language (SQL). Developer has to find out the efficient way to access database, i.e. to select effective query from a number of queries to perform same task.  
Hibernate provides a powerful query language Hibernate Query Language (independent from type of database) that is expressed in a familiar SQL like syntax and includes full support for polymorphic queries. Hibernate also supports native SQL statements. It also selects an effective way to perform a database manipulation task for an application.  
Application using JDBC to handle persistent data (database tables) having database specific code in large amount. The code written to map table data to application objects and vice versa is actually to map table fields to object properties. As table changed or database changed then it’s essential to change object structure as well as to change code written to map table-to-object/object-to-table. 
Hibernate provides this mapping itself. The actual mapping between tables and application objects is done in XML files. If there is change in Database or in any table then the only need to change XML file properties.  
With JDBC, it is developer’s responsibility to handle JDBC result set and convert it to Java objects through code to use this persistent data in application. So with JDBC, mapping between Java objects and database tables is done manually.  
Hibernate reduces lines of code by maintaining object-table mapping itself and returns result to application in form of Java objects. It relieves programmer from manual handling of persistent data, hence reducing the development time and maintenance cost.  
With JDBC, caching is maintained by hand-coding.  
Hibernate, with Transparent Persistence, cache is set to application work space. Relational tuples are moved to this cache as a result of query. It improves performance if client application reads same data many times for same write. Automatic Transparent Persistence allows the developer to concentrate more on business logic rather than this application code.  
In JDBC there is no check that always every user has updated data. This check has to be added by the developer.  
Hibernate enables developer to define version type field to application, due to this defined field Hibernate updates version field of database table every time relational tuple is updated in form of Java class object to that table. So if two users retrieve same tuple and then modify it and one user save this modified tuple to database, version is automatically updated for this tuple by Hibernate. When other user tries to save updated tuple to database then it does not allow saving it because this user does not have updated data.       

Object states in hibernate

This summary is not available. Please click here to view the post.

Why do use lazy="false"?


It decides whether the child object is loading to the parent or not.
  1. We have to set it in the mapping file of the parent class.If lazy="true" (means not to load child)
  2.  By default the lazy loading is true.
  3.  But some cases we need to load the child object when the parent is loaded.
  4.  So in that case we'll have to write lazy="false".

What is preinitialization of a java servlet?

In the java servlet life cycle, the first phase is called ‘Creation and intialization’.

The java servlet container first creates the servlet instance and then executes the init() method. This initialization can be done in three ways. The default way is that, the java servlet is initialized when the servlet is called for the first time. This type of servlet initialization is called lazy loading.

The other way is through the <load-on-startup>non-zero-integer</load-on-startup> tag using the deployment descriptor web.xml. This makes the java servlet to be loaded and initialized when the server starts. This process of loading a java servlet before receiving any request is called preloading or preinitialization of a servlet.

Servlet are loaded in the order of number(non-zero-integer) specified. That is, lower(example: 1) the load-on-startup value is loaded first and then servlet with higher values are loaded.

Example usage:
<servlet>
       <servlet-name>Servlet-URL</servlet-name>
       <servlet-class>com.javapapers.Servlet-Class</servlet-class>
       <load-on-startup>2</load-on-startup>
</servlet>

Can we make hashMap synchronized?

Yes, we can make HashMap to be synchronized.
Map hashMap = new HashMap();
Map m = Collections.synchronizedMap(hashMap);

Types of Inner Class / Nested Class in Java

 There are four type of inner classes in java. There briefly,
  1. Static inner class
  2. Non static inner class
  3. Local inner class
  4. Anonymous inner class
The class which is declared with inside class is called nested class / inner class. This is classified to two types. There are, Static Nested Class & Non Static Nested Class / Inner Class.
1. Static Nested Class:
  • Nested class which is declared static is static nested class.
  • Its methods & members associates with outer class.
  • It can be used through object reference.
  •  Example:
    OuterClass.StaticNestedClass innerStatic = new OuterClass.StaticNestedClass();  
               innerStatic.display(); 
Example for Static Nested Class
 class OuterClass{  
      // Declare static nested inner classs  
      static class StaticNestedClass{  
           //static inner class method  
           void display(){  
                System.out.println("Inside Static Nested Class");  
           }  
      }  
      //outer class method  
      void display(){  
           System.out.println("Outer Class");  
      }  
 }  
 class MainClass  
 {  
      public static void main(String[] arg)  
      {  
           //how to access static nested class method  
           //1st create object for outer class object  
           OuterClass outer = new OuterClass();  
           outer.display();  
           //Now calling static nested class   
           OuterClass.StaticNestedClass innerStatic = new OuterClass.StaticNestedClass();  
           innerStatic.display();  
      }  
 }  

2. Non Static Nested Class / Inner Class:
  • It has access to the other members of the outer class, even they are private.
  • As the static declaration are not allowed inside inner class, we can't run inner class directly console.
  • We can access inner class code through outer class object only.
  • Instance of inner class valid within the enclosed class & has direct access to the methods & members of the enclosed instance.
  • For example,  
    OuterClassNonStat.NonStaticNestedClass inner = outer.new NonStaticNestedClass();
 Example for Inner Class:
 class OuterClassNonStat{  
      // Declare static nested inner classs  
      class NonStaticNestedClass{  
           //non static inner class method  
           void display(){  
                System.out.println("Inside Non Static Nested Class");  
           }  
      }  
      //outer class method  
      void display(){  
           System.out.println("Outer Class");  
      }  
 }  
 class MainClassNonStatic  
 {  
      public static void main(String[] arg)  
      {  
           //how to access non static nested class method  
           //1st create object for outer class object  
           OuterClassNonStat outer = new OuterClassNonStat();  
           outer.display();  
           //Now calling non static nested class   
           OuterClassNonStat.NonStaticNestedClass inner = outer.new NonStaticNestedClass();  
           inner.display();  
      }  
 }  

 3. Local Inner Class:
  • Local inner class is class declared in the body of method. Such class is know within contains method only. It is called Local Inner Class.
  • If we are declaring local inner class inside method, we can access both instance and static variables of outer class directly from the inner class.
  • From local inner class we are not allowed to access the local variables of the method unless it is marked as final.
  • Its not a member of enclosing class, hence doesn't have a access specifiers.
  • These inner class are very rarely used classes in real time. Because, these inner classes can be accessed with in the method only. Outside the method, your not allowed to access, violation leads to CTE.
Example for local inner class:
 class OuterClass {  
   static int x=10;  
   void m1()  
   {  
           int y=20;  
           class LocalInnerClass  
           {  
                void show()  
                {  
                     System.out.println(x); // 10  
                     System.out.println(y); // Compile time error ( If y is final no error)  
                }  
           }  
           LocalInnerClass inner=new LocalInnerClass();  
           inner.show();  
   }  
 }  
 public class MainLocalInnerClass {  
      public static void main(String args[]) {  
           OuterClass outer = new OuterClass();  
     outer.m1();  
      }  
 }  

4. Anonymous Inner Class:
  • We can declare anonymous inner class either in static or non static methods.
  • If we declare anonymous inner class in the static method the static variables of outer class can be accessed directly from inner class.
  • If we are declaring anonymous inner class inside instance method, we can access both instance and static variables of outer class directly from the inner class.
  • From anonymous inner class we are not allowed to access the local variables of the method unless it is marked as final.
  • It is accessible only at the point of its declaration.
 Example for anonymous inner class:
 interface Inter {  
      void display();  
 }  
 class AnonymousInnerClass  
 {  
      public static void main(String arg[]){  
           //Create thread using anonymous class  
           int x = 10;  
           Runnable r=new Runnable(){  
                public void run(){  
                     System.out.println("run"+x);//compile error.  
                               //local variable x accessed from inner class (if x is final then no error)  
                }  
           };  
           Thread t=new Thread(r);  
           t.start();  
           //implement interface  
           Inter inter = new Inter(){  
                public void display(){  
                     System.out.println("Display method");  
                }  
           };  
           inter.display();  
      }  
 }  

Live Trend: Earthquake in Chennai, Thiruvallur Today Apr 11-4-...

Live Trend: Earthquake in Chennai, Thiruvallur Today Apr 11-4-...: - Mild Earthquake in some parts of Chennai at around 2.15 pm IST today April 11 2012 (Wednesday) - Tremor was also felt at Tiruvallur - Pe...

Difference between a Vector and an Array in Java


  1. Vector is a dynamic array, whose size can be increased, where as an array size can not be changed.
  2. Reserve space can be given to Vector, where as array can not.
  3. Vector is a Class, where as array is not.
  4. Vector can be stored any type of objects, where as an array can stored only homogeneous values.
  5. Vector implements List interface, where as an array is primitive data type.
  6. Vector is synchronized, where as array is not.

Difference between class variable and instance variable in java


Consider the following class:
public class IdentifyMyParts {
    public static int x = 7;
    public int y = 3;
} 
  1. Question: What are the class variables?
    Answer: x
  2. Question: What are the instance variables?
    Answer: y
  3. Question: What is the output from the following code:
    IdentifyMyParts a = new IdentifyMyParts(); 
    IdentifyMyParts b = new IdentifyMyParts(); 
    a.y = 5; 
    b.y = 6; 
    a.x = 1; 
    b.x = 2; 
    System.out.println("a.y = " + a.y); 
    System.out.println("b.y = " + b.y); 
    System.out.println("a.x = " + a.x); 
    System.out.println("b.x = " + b.x); 
    System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);
    
    Answer: Here is the output:
     a.y = 5 
     b.y = 6 
     a.x = 2 
     b.x = 2
     IdentifyMyParts.x = 2
    
    Becausexis defined as apublic static intin the classIdentifyMyParts, every reference toxwill have the value that was last assigned becausexis a static variable (and therefore a class variable) shared across all instances of the class. That is, there is only onex: when the value ofxchanges in any instance it affects the value ofxfor all instances ofIdentifyMyParts

JSP useBean and Scope Types

The beans can be declared in JSP as following: Scope parameter indicates the context in which the bean should be made available. There are four different possible kinds of useBean scope, “page” scope being the default one.
  1. page: Availability: The bean is only available on the current page Storage:The bean is stored in the PageContext of the current page.
  2. request: Availability: The bean is only available for the current client request Storage: The bean is stored in the ServletRequest object. 
  3. session: Availability:The object is available to all pages during the life of the current HttpSession. Storage: The bean is stored in HttpSession object. 
  4. application: Availability: The bean available to all pages that share the same context. Storage: The bean is stored in ServletContext object