Sunday, 2 February 2014

Introduction to Hibernate


  •  Hibernate is an open source object/relational mapping tool for Java.
  • Hibernate is Free Software. The LGPL license is sufficiently flexible to allow the use of Hibernate in both open source and commercial projects (see the LicenseFAQ for details). Hibernate is available for download at http://www.hibernate.org/. 
  • Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities and can significantly reduce development time otherwise spent with manual data handling in SQL and JDBC.
  • Hibernates goal is to relieve the developer from 95 percent of common data persistence related programming tasks.
More Info

Friday, 15 June 2012

Collections related performace tips in java

Optimization techniques in Lists



List types represent an ordered collection of objects. ArrayList, Vector, Stack and LinkedList are the List implementation classes. All  List types support basic operations - adding objects, removing objects, accessing objects and iterating through the list. So which one to choose since all the list implementations support these basic operations? Performance is different for each class based on specific operations. So your choice is driven by the performance and the requirement options. Your requirement could be
  1. Thread safe collection
  2. Size of collection (large or small collection)
  3. Type of operation ( adding, removing, accessing or iterating )
If you want your collection to be thread safe then Vector or Stack must be used because both have synchronized methods. While ArrayList and LinkedList are not thread safe. Stack is meant for specific LIFO (last in - first out) operations, this can be filtered down based on this specific requirement. If you don't want your collection to be thread safe then you have can choose between ArrayList or LinkedList. General concept from performance point of view is that ArrayList gives better performance when accessing and iterating objects whereas LinkedList gives better performance when adding and removing objects. Although true in most cases, sometimes there is an exception.

Reason 

  1. JDK 1.3 gives best performance because of HotSpot Virtual Machine.
  2. The initial size for ArrayList and Vector is 10. ArrayList increases its capacity by half approximately whenever its capacity reaches maximum (10) but Vector increases its capacity by double whenever its capacity reaches maximum. That is the reason why ArrayList takes more time than Vector if it is not initialized with proper size though ArrayList is not synchronized. As soon as it reaches its maximum capacity when adding objects, it creates one more bigger array ( with 15 capacity for ArrayList approximately and 20 capacity for Vector) and copies the previous and new objects into new array. Obviously it is expensive to create new array and copy objects. So best approach is to initialize the ArrayList and Vector with proper size using constructors or using ensureCapacity(int capacity) which gives good performance.  If you initialize with proper size then the ArrayList gives better performance than Vector. ArrayList with initialization gives better performance than others because its methods are non-synchronized. Synchronized methods are bit expensive because JVM has to lock the objects whenever it finds synchronized methods. Vector takes slightly more time than ArrayList when you use JDK1.3 Hotspot JVM ,if you are not sure that whether your collection needs to be thread safe or not then it is better to use Vector to have higher safety.You can convert an ArrayList as thread safe collection using Collections.synchronizedList(ArrayList object) but it is more expensive than using a Vector.
  3.  ArrayList and Vector maintain internal Object array ( Object[]) to store objects. So whenever you add an object, they add it to the end of the array which is  fine as long as it doesn't reach its maximum capacity. If you want to add an object at any other position, it creates a new object array and recopies all the objects which is expensive. That is the reason why adding objects at middle and beginning of collection takes a long time than when it is adding at the end.
  4.  LinkedList gives good performance when adding elements at the end and beginning but it is worse when adding objects at middle because it needs to scan the node whenever it needs to add an object. LinkedList cannot be initialized. 

Removing objects:

  1. All classes take approximately same time when removing objects from end
  2. ArrayList and Vector give similar performance with slight difference because of JDK1.3 Hotspot JVM.
  3. LinkedList  gives worst performance when removing objects from middle (similar to adding objects at middle).
  4. LinkedList gives better performance when removing objects from the beginning.
  5. Only LinkedList gives better performance when removing objects from the beginning. 

Accessing objects:

  1. ArrayList and Vector give best performance because they access objects using index. Vector takes slightly more time but it is negligible.
  2. LinkedList gives worst performance  when accessing objects at end and middle because it has to scan nodes to access objects.

Lists:

  1. Use ArrayList with proper initialization if you don't want thread safe for the collection whenever you  add/remove/access objects at end and middle of collection.
  2. Use Vector with proper initialization if you want thread safe for the collection whenever you  add/remove/access objects at end and middle of collection.
  3. Use LinkedList if you don't want thread safe for the collection whenever you  add/remove/access objects at beginning of collection.
  4. Use synchronized LinkedList if you want thread safe for the collection whenever you add/remove/access objects at beginning of collection.
  5. Use ListIterator than Iterator and Enumeration for List types

Sets

  1. Use HashSet for maintaining unique objects if you don't want thread safe for the collection for all basic(add/remove/access) operations otherwise use synchronized HashSet for thread safe.
  2. Use TreeSet for ordered and sorted set of unique objects for non-thread safe collection otherwise use synchronized TreeSet for thread safe
Maps
  1. Use HashMap for non-thread safe map collection otherwise use Hashtable for thread safe collection.
  2. Use TreeMap for non-thread safe ordered map collection otherwise use synchronized TreeMap for thread safe.

For more information : Precise Java

Thursday, 1 December 2011

Fetch value from two more table and insert into another table using cursor in Oracle

1. Create table1 contains column with empid.

2. Create table2 contains column with firstname,lastname, empid(reference with table1 empid)

3. Create table3 contains column with empid,firstname,lastname

4. Add some data into table1 and table2.

5. Finally Run the below command:

DECLARE
CURSOR MyCursor IS
SELECT t1.empid,t2.firstname,t3.lastname
from table1 t1,table2 t2 WHERE t1.empid>100;
BEGIN
FOR MyCursors IN MyCursor LOOP
INSERT INTO table3 (empid,firstname,lastname)
VALUES (MyCursors.empid,MyCursors.firstname, Mycursors.lastname);
END LOOP;
COMMIT;
END;