Thursday, October 27, 2011

Hibernate

About Hibernate in a line : Hibernate applications define persistent classes that are "mapped" to database tables. 

Saving data to a storage is called persistence.

Hibernate is an object-relational mapping (ORM) library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database. Hibernate solves object-relational impedance mismatch problems by replacing direct persistence-related database accesses with high-level object handling functions

Hibernate's primary feature is mapping from Java classes to database tables (and from Java data types to SQL data types). Hibernate also provides data query and retrieval facilities. 

Hibernate generates the SQL calls and attempts to relieve the developer from manual result set handling and object conversion and keep the application portable to all supported SQL databases with little performance overhead.

Mapping :
Mapping Java classes to database tables is accomplished through the configuration of an XML file or by using Java Annotations. When using an XML file, Hibernate can generate skeletal source codefor the persistence classes. This is unnecessary when annotations are used.

Facilities to arrange one-to-many and many-to-many relationships between classes are provided. In addition to managing associations between objects, Hibernate can also manage reflexiveassociations where an object has a one-to-many relationship with other instances of its own type

Persistence :
Hibernate provides transparent persistence for Plain Old Java Objects (POJOs). The only strict requirement for a persistent class is a no-argument constructor, not necessarily public. Proper behavior in some applications also requires special attention to the equals() and hashCode() methods

Collections of data objects are typically stored in Java collection objects such as Set and List. Java generics, introduced in Java 5, are supported. Hibernate can be configured to lazy load associated collections. Lazy loading is the default as of Hibernate 3

HQL (Hibernate Query Language) :
Hibernate provides an SQL inspired language called Hibernate Query Language (HQL) which allows SQL-like queries to be written against Hibernate's data objects

Integration :
Hibernate can be used both in standalone Java applications and in Java EE applications using servlets or EJB session beans. It can also be included as a feature in other programming languages. For example, Adobe integrated Hibernate into version 9 of ColdFusion (which runs on J2EE app servers)

Hibernate Annotation and many to One mapping, one to many etc..cascade is the key :) http://www.vaannila.com/hibernate/hibernate-tutorial/hibernate-tutorial.html
http://www.mkyong.com/tutorials/hibernate-tutorials/

Cascade  (cascade = CascadeType.ALL) All means everything includes save, create, delete, update: If you have a parent and child objects, if you use cascade saving parent object will save the child objects with in the parent as well.

cascade=”delete-orphan” : delete-orphan allow parent table to delete few records (delete orphan) in its child table. Good to have cascade="delete-orphan"

The cascade=”delete” is declared in ‘stockDailyRecords’ to enable the delete cascade effect. When you delete the ‘Stock’, all its reference ‘stockDailyRecords’ will be deleted automatically

Hibernate interceptors : http://www.mkyong.com/hibernate/hibernate-interceptor-example-audit-log/
Interceptors : If we hibernate save onSave we can call method and we can do something. onDelete we can call method and we can do something. When the operation get performed we can intercept and ask the method to do some action.


fetch = FetchType.LAZY - 
Lazy - Defines that data can be lazily fetched
Eager - Defines that data must be eagerly fetched
Lazy will make object lighter. It's like a proxy. When the get method called it will hit db and fetch the record.
Eager - Eager will have the data already in the object. If you want to avoid the database hit use Eager. Otherwise, lazy. For small objects use eager. Big objects use lazy

HQL - Hibernate Query Language. Its same as SQL. HQL uses class name instead of table name, and property names instead of column name.

In HQL, only the INSERT INTO … SELECT … is supported; there is no INSERT INTO … VALUES. HQL only support insert from another table. For example
"insert into Object (id, name) select oo.id, oo.name from OtherObject oo";
Insert a stock record from another backup_stock table. This can also called bulk-insert statement.
Query query = session.createQuery("insert into Stock(stock_code, stock_name)" +
       "select stock_code, stock_name from backup_stock");
int result = query.executeUpdate();

Hibernate parameter binding examples

Always bind a parameter. Never concatenate the parameter on HQL. Concat is bad code. User can given anything as input and its SQL Injection may happen.
Named parameters
Parameter for the HQL is passed with property name with colon (:property_name)
Ex query : 
Query query = session.createQuery("From Employee WHERE departmentName=:dName");
query.setParameter("dName", "Marketing");
In setParameter you need to pass the named parameter as key.
setParameter - smart enough to discover the parameter data type for you 
setString - You can use setString if you know its a string
setProperties - you can pass object into the setProperties. Based on the parameter propertyName hibernate identifies it
Positional parameters
It’s use question mark (?) to define a named parameter, and you have to set your parameter according to the position sequence. See example…
String hql = "from Stock s where s.stockCode = ? and s.stockName = ?";
List result = session.createQuery(hql)
.setString(0, "7277")
.setParameter(1, "DIALOG")
.list();
Hibernate Transaction - You can rollback or commit the changes with hibernate
Hibernate Native SQL - You can use SQL directly using hibernate
Hibernate Named Query - You can declare the HQL with Name and you can use it across the class


Hibernate Performance tweaks:
dynamic-insert = true - Default is false. Dynamic insert true will make you insert only the object data which  exist values. Null values will not be constructed to insert into db. Make show_sql true to   see it.
dynamic-update = true - Default is false. Dynamic update true will update only the records you updating in the object rather getting saved the whole object data. 
invert="true" : (inverse - Opposite) : If you have parent and child objects, if you use inverse="true" saving parent object will not save child object with in the parent.


mutable="true" : We can change mutable as "false" it means the updates to this class will be ignored, but no exception is thrown, only the add and delete operation are allow. Mutable in collection add and delete-orphan are not allow in this collection, with exception throw, only update and ‘cascade delete all’ are allow. 


get and Load - Load will give the object with proxy (hibernate term) with data. It will not hit the db. IF the object not found you get NPE. 
get - It will hit the db and fetch the data. If data not exist null will be there.


Use Fetching strategies if you have parent child objects (http://www.mkyong.com/hibernate/hibernate-fetching-strategies-examples/
1. fetch-”join” = Disable the lazy loading, always load all the collections and entities.
2. fetch-”select” (default) = Lazy load all the collections and entities.
3. batch-size=”N” = Fetching up to ‘N’ collections or entities, *Not record*.
4. fetch-”subselect” = Group its collection into a sub select statement.


softDelete - 


How to auto create hbm files, java files and DAO files.

Provide this link in Eclipse->Help->Install New Software. Add it and install. So, all the dependencies will also get installed : http://download.jboss.org/jbosstools/updates/development/indigo/
Open a workspace for Hibernate Auto generation of hbm, java and DAO's.
Create a JAVA project with the ProjectName (Student | HibernateTool)
Windows -> Open Perspective -> Other -> select `Hibernate`
Write click in Hibernate explorer and click 'Add Configuration'
Click New on 'Database Connection'. Add a database connection (ex : mysql ). Go Next and add a Driver (JDBC Driver) based on the mysql connector java jar version you have. Remove the default jar. Add the jar and create driver and connection. Test the Connection with databasename to check all is well.
In the configuration point the directory under Project.
You can see all the database with data imported into your box.
Select `Hibernate code Generation Configuration` 
Click 'New' 
Select console configuration. Output Directory. Check 'Reverse Engineer from JDBC Connection'. Provide package name. Select 'reveng.xml new'.
In Exporter select 'Domain Code, .hbm.xml, Run.
Select hibernate in console configuration. 
Under project you can see the hbm and Domain Objects.


one_to_many

Always one will lend to many.
 
Hibernate Performance BottleNecks on Bulk Insert:
  
 
 Hibernate is problematic when you do bulk insert. You should use JDBC to avoid.
Bulk inserts of data are a type of operation best not performed by
Hibernate. For example, a user may have 100,000 records that have to be imported
into a single table. Don't use Hibernate for this sort of
operation—use your database's built-in import tools instead. The
built-in import will be faster than Hibernate (or, for that matter, handwritten
JDBC). 
 
MySQL Dialect doesn't support sequences. It supports only identity. identity makes hibernate not to work on bulk_insert.


If, for some reason, you do need to do a bulk import via Hibernate, take
account of the following tips:
  • Make sure the hibernate.jdbc.batch_size option (specified in your hibernate.properties, as described in Chapter 6) is turned on and set to a reasonably large value.
  • Consider Session.commit() on to break up the transactional overhead. Presumably you will do this only if you are very confident that it will succeed.
  • Make sure that you call Session.close() it or Session.clear() after each call to Session.commit(). Otherwise, Hibernate will attempt to maintain the inserted object in the session-level cache.
  • Consider the seqhilo (generator : hilo) or assigned generator to optimize key generation.

    Note :  Batch insert will be disabled if your auto generator value is based on mysql server and if mysql has to respond.

    Enable MySQL logs to see the queries :
    In MySQL prompt add below mysql>. Check 'show variables' to see earlier values
    SET global log_output = 'FILE';
    SET global general_log_file='/var/log/mysql.log';
    SET global general_log = 1;
     
    Note : Provide permission as "chown mysql:root " and "chmod 755 " for the same file.
    
    
    When you do bulk insert through webserver : 
    jdbc:mysql://dbserver/database?rewriteBatchedStatements=true&relaxAutoCommit=true

    Code : 
    try {
    autoCommit = getSession().connection().getAutoCommit();
    getSession().connection().setAutoCommit(false);
    Transaction tx = getSession().beginTransaction();
    for (MyObject object : objectList){
    getSession().saveOrUpdate(object);
    }
    tx.commit();
    }
    catch (.. ..){
    ...
    }

     
    Refer: http://upcom.eu/batch-inserts-in-hibernate/ 

Tuesday, October 25, 2011

Java - Interview Questions


Interview Question links :

Spring : http://www.javabeat.net/articles/103-spring-framework-interview-questions-1.html


Java Interview Questions : http://www.allapplabs.com/interview_questions/java_interview_questions.htm#q1
http://www.techinterviews.com/master-list-of-java-interview-questions

MySQL Interview Questions : 
http://www.techinterviews.com/29-mysql-interview-questions
http://www.techinterviews.com/31-more-mysql-questions


Interface - Interface is a signature. You need to implement the methods in interface.

Abstract class - Abstract class can have abstract methods and normal private, protected methods. You need to extend the abstract class. You can use the super class methods. Abstract methods needs to implement.

class - blueprint of object

package - is a collection of classes with the given namespace

Inheritance - b is a child of a (ISA) child can inherit all the method from parent. You need to use extend

passbyreference : passing the address  passbyvalue : passing the value

synchronization in respect to multithreading - synchronization is the capability to control the access of multiple threads to shared resources. Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable.

Multithreading : If there is a for loop iterator 1 thread can loop it and another thread can add it to the loop

HashMap and Map - Map is Interface and Hashmap is class that implements that. HashMap (can store keys and values). HashMap will allow nulls as keys and values. Having said it doesn't allow duplicate values.

TreeMap : Treemap will have mutiple maps with parent and child maps. TreeMap is implemented based on SortedMap interface. Its slower than HashMap. The value stored like a tree. In whatever way the value stored the same way from Map you can retreive. HashMap the values will not be stored as we saved.

final - A final class can't be extended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant)

Dispatcher servlet - It will delegate the request comes from web

HashMap and HashTable - HashMap is unsynchronized. Hashmap allows null in keys and values. Hashtable is syncrhonized. Synchronized is overhead unless its needed. If needed you can explicitly mention synchronized { }

Vector and ArrayList - ArrayList is unsynchronized. Arraylist is a class. You can add list of data. Vector is a synchronized.

Set - Set is also a collection. You can have group of objects / data which can be unique.

Collection - collection is a framework

Swing and Awt - AWT (Abstract window toolkit)  are heavy-weight componenets. Swings are light-weight components. Hence swing works faster than AWT.

equals and hashcode - is to compare to objects. IF there were 2 objects in same name to compare the object. In which object the names to compare you can mention in equals and compare

static class loading - new classname { dynamic class loading - class.ForName

Marker interface - no methods its used for clonable

Comparator compare(a,b) and Comparable a.compareTo(b) - Both interfaces.

immutable - The normal way we write string is immutable. a= "test" and a = "gubs". To object getting instantiate and reference getting change. StringBuffer is a mutable. When we assign the data it change the value in the same variable without instantiate.

Threads - Extended thread class, implement the runnable interface

Casting  - convert the type of the data

Design pattern  - ? singleton - ? how it works

Thread - ? how it works and examples ?

spring framework - how it works with example

hibernate - what is hibernate ? namedsqlquery ? update ? merge ?

Enhanced For loop - for (Object a : as) {} iterator for loop. Old style for loop

Get int / short value from String - parseInt or parseShort Short.parseShort(String); Integer.parseInt(String) Or Short.valueOf(String)

Convert short value to int value : new Integer(shortVal);

JAR File - Java Archive Files (It contains all the JAVA Classes for the specific module)

log4j properties load run time
-Dlog4j.configuration=file:/path/log4j.properties

WAR File - Web Application Archive (Contains all the JARs, JSP (JavaServer Page), Java Servlets, JAVA Classes , XML files, tag libraries and static Web pages (HTML and related files) that together constitute a Web application.


  • ArrayList: Take the data and pass it to the calling method, ignore duplicates and nulls.
  • HashSet: Take the data and pass it to the calling method, ignore the sequence, but make sure there are no duplicates.
  • LinkedHashSet: Take the data and pass it to the calling method, maintain the sequence and avoid duplicates.

J2EE - Java 2 Platform Enterprise Edition

j2ee - java 2 platform enterprise edition
  • j2ee is widely used for server programming and web-based design application
  • j2ee platform consists of services, api's and protocols to-gether which provides functionality to deploy fault-tolerant, distributed, multi-tier.
  • j2ee has in-build components of JDBC, JSP, java applets, EJB (Enterprise java beans - seperate layer to write business logic), Java servlet API

Distributed computing - A distributed system consists of multiple autonomous computers that communicate through a computer network. In distributed computing, a problem is divided into many tasks, each of which is solved by one or more computers.

Multitier architecture - is a client–server architecture. The most widespread use of multi-tier architecture is the three-tier architecture (presentation tier (User Interface), logic tier (Business logics and fuctionalities), and a data tier (data storage and data access from db)) The three-tier model is a software architecture and a software design pattern.

Advantage : By breaking up an application into tiers, developers only have to modify or add a specific layer, rather than have to rewrite the entire application over.

Multitier Comparison with the MVC architecture : Conceptually the three-tier architecture is linear: However, the MVC architecture is triangular (the view sends updates to the controller, the controller updates the model, and the view gets updated directly from the model.) A fundamental rule in a three tier architecture is the client tier never communicates directly with the data tier; in a three-tier model all communication must pass through the middle tier

Fault-tolerant design - fault-tolerant design is a design that enables a system to continue operation, possibly at a reduced level rather than failing completely. In hardware / software we used to increase the response time. An example in another field is a motor vehicle designed so it will continue to be drivable if one of the tires is punctured. We need to use only for critical operations.

In Operative.One (our software) we used in SalesOrder exports and finance billing exports. Results execute at reduce level to client rather failing the whole operation. Faults will be logged in the log and user can download the failures to verify.

Operative.One (Hardware) : When we have multiple jetty server (jetty01, jetty02) even if 1 jetty server fails another server will perform the operation.

Disadvantages : Reduction of priority of fault correction, Test difficulty, cost (Hardware)

Framework : 
Software framework, a reusable set of libraries or classes for a software system (or subsystem)

  • Application framework, a software framework used to implement the standard structure of an application for a specific operating system
  • Web application framework, a software framework for development of dynamic websites, web applications, and web services
  • Conceptual framework, a set of theories widely accepted enough to serve as the guiding principles of research within a particular discipline.


Saturday, October 22, 2011

Sample JDBC Program

Download mysql-connector-java-5.1.12-bin.jar. Include mysql jar in your project. (Go to Eclipse Menu 'Project -> Properties -> Java Build Path -> Libraries -> 'Add External JARs' add downloaded jar)
package testJdbcPackage;

import java.sql.*;

public class TestJdbc {
 
 Connection conn;
 
 public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
  new TestJdbc();
 }
 
 public TestJdbc() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
  String url = "jdbc:mysql://localhost/test";
  String user = "root";
  String password = "mysql";
  try {
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   conn = DriverManager.getConnection(url, user, password);
   doInsertTest();
   doDeleteTest();
   doInsertTest();
   doSelectTest();
  } catch (SQLException e) {
   e.printStackTrace();
  }  
 }
 
 private void doDeleteTest() {
  try {
   Statement statement = conn.createStatement();
   //Get the count
   ResultSet rs = statement.executeQuery("SELECT COUNT(1) FROM student");
   int recordCount = 0;
   while(rs.next()) {
    recordCount = rs.getInt("COUNT(1)");
   }
   rs.close();
   if (recordCount > 1) {
    recordCount -= 1 ;
   }
   boolean defaultAutoCommit = conn.getAutoCommit();
   conn.setAutoCommit(false);
   try {
    statement.executeUpdate("DELETE from student LIMIT " + recordCount);
    conn.commit();
   } catch (Throwable e) {
    conn.rollback();
   } finally {
    conn.setAutoCommit(defaultAutoCommit);
   }   
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }  
 }

 private void doSelectTest() {
  Statement statement;
  try {
   statement = conn.createStatement();
   ResultSet rs = statement.executeQuery("SELECT * from student");
   while(rs.next()) {
    int columnCount  = rs.getMetaData().getColumnCount();
    for (int i = 1; i <= columnCount; i++) {
     System.out.println(rs.getObject(i));
    }
   }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }    
 }

 private void doInsertTest() {
  Statement statement;
  try {
   statement = conn.createStatement();
   statement.executeUpdate("INSERT INTO student(name, status, deleted) VALUES('Gubs', 'active', 0)");
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }    
 }
}
// Below script tag for SyntaxHighLighter