Tuesday, May 13, 2014

Linear Regression with Multiple Variables using R language

I tried here to derive Linear Regression with Multiple Variables , the same I tried with OCTAVE earlier.

I have a data-set as follows-

Loading the above data in R, can be done -
 mydata = read.table("D:/tmp/mlclass-ex1-005/mlclass-ex1-005/R-Studio/data.txt",header=TRUE,sep=",")  

Now as we know , we should scale our data , as each of the parameters are different in scale. So we will be doing Normalization.

 scale = function(dta,cols,counts){  
  for(i in 1:counts){  
   #As we didn't create matrix , the way to fetch column-wise record is as follows-  
   value = dta[cbind(seq_along(1:nrow(mydata))),i];  
   sigma = sd(value);  
   mu = mean(value);  
   dta[paste(cols[i], ".scale", sep = "")] = (value - mu)/sigma;  
   #Will append .scale after each processing  
  }  
  return (dta);  
 }  


 the Gradient Descent algorithm





where alpha is learning rate which needed to be analysed, so here we'll be running interpretation on some set of alphas-

 alpha = c(0.03, 0.1, 0.3, 1, 1.3, 2,0.4,0.2)  

For doing that we need to know about cost Function
What is Cost Function
The idea is look at how the Cost value J(alpha) drops with the number of iterations, the fastest the drop the better, but if goes up then the alpha value is already too large.

So I derived the formula here-

 # the cost for a given theta  
 cost = function(x,y,th,m) {  
  prt = ((x %*% t(th)) - y)  
  return(1/m * (t(prt) %*% prt))  
 }  

Once we done with above we are now ready for running the

Now deriving the Vectorization formula, predict the delta as follows-

 # the delta updates  
 delta = function(x,y,th) {  
  delta = (t(x) %*% ((x %*% t(th)) - y))  
  return(t(delta))  
 }  

Now run each of the alpha or learning rate on cost function-

 # run J for 50x, on each alpha  
 for (j in 1:length(alpha)) {  
  for (i in 1:50) {  
   J[i,j] = cost(x,y,theta,m) # capture the Cost  
   theta = theta - alpha[j] * 1/m * delta(x,y,theta)  
  }  
 }  


Once we are done with cosFunction run , we can plot the graph to get the results-

 # lets have a look  
 par(mfrow=c(length(alpha)/2,2))  
 for (j in 1:length(alpha)) {  
  plot(J[,j], type="l", xlab=paste("alpha", alpha[j]), ylab=expression(J(theta)))  
 }  


I got a graph as follows-



From the above , I derived that 0.4 suits my need the best as the graph converged to minimum the fastest and it was idle after that.


Now for finalizing the learning rate alpha, I must run it until convergence so I estimated it till 50000

 for (i in 1:50000) {  
  theta = theta - 0.4 * 1/m * delta(x,y,theta)  
  if (abs(delta(x,y,theta)[2]) < 0.0000001) {   
   break # to interrupt updates  
  }  
 }  

Now I am able to predict the the value-

 # 2. The predicted price of a house with 2000 square feet and 3 bedrooms.  
 # Don't forget to scale your features when you make this prediction!  
 print("Prediction for a house with 2000 square feet and 3 bedrooms:")  
 #value = dta[cbind(seq_along(1:nrow(mydata))),1];  
 s <- dta[cbind(seq_along(1:nrow(mydata))),1]; #Size  
 l <- dta[cbind(seq_along(1:nrow(mydata))),2]; #location  
 b <- dta[cbind(seq_along(1:nrow(mydata))),3]; #Number of Bedrooms  
 f <- dta[cbind(seq_along(1:nrow(mydata))),4]; #Floor Number  
 print(theta %*% c(1, (2000 - mean(s))/sd(s), (2 - mean(l))/sd(l),(1 - mean(b))/sd(b),(2 - mean(f))/sd(f)))  


All the source code , can be found in my GDrive repository as well.
Please contact me for the link or add comment but almost everything is there above.

Wednesday, May 7, 2014

Machine Learning - 2 (Basics , Cost Function)

part 1 - Machine Learning 1


Machine learning Basics




TRAINING SET
In a machine learning algorithm, we have a training set, on which an operation happens which is called learning algorithm and based on the learning it gives a output named a hypothesis.





So if we consider the example of supervised algorithm Property example, so by inputting x as any square feet of house, h will be producing a real output for me.

Representation of h (hypothesis\response surface)

UNIVARIATE LINEAR REGRESSION







So from above, h can be deduced as linear function as property problem can be solved from linear equation.










The above property model uses linear function of h.
This model is known as Linear regression with one variable or univariate linear regression.

Cost Function


Cost Function determines how to fit the best possible straight line into our data.
So from the above example if you have number of datasets we need to figure out where exactly the straight line must be drawn or it will be great trouble to get the right result.











So from the above, we can conclude following- for any value of x what will be the value function will be giving which will be finally the value of y.
1.      For any value of x , value of Y remains constant as Ɵ1x with 0 becomes 0 , so there is no multiple of x. So for any value of X , value of Y is just 1.5 , no change throughout.. So h(x) is nothing but a value of Y wrt to x and since x already became 0, y remained constant. So property price will never change based on plot size increase.
2.      For any value of x , value of y will be half. Here for value of x , value of y ideally will be 1 , if we are going head to head , but what happened here is for any value of x , value of y is half of the value of so property rate won’t be increased as high if the size of property getting bigger and bigger.
3.      For any value of x, value of y will be 1.5. So property price will as well get increased as size of the property increases.

         

UNDERSTANDING COST FUNCTION

Cost function is directly proportion to performance of the predicted response. It is an extremely important topic for that sake.




In a supervised learning problem, we can divide the tasks in 3 categories-

1.      A collection of n-dimensional feature vectors, because n-dimension input like A property details carrying plot size, number, construction size, location etc. Each property is a dimension.
{Xi}, I = 1 …n


2.      A collection of observed responses like in our case, property price is the observed response
{yi},I = 1 ….. n

3.      A predicted output ie response surface or hypothesis
h(x)






Above are data sets with having plot details property. Here X is having P dimension and each dimension can be number, name, loc, size etc. Y is the response output with some number or defined output attribute.
So the h(x) can draw certain conclusion about y .
·        So here we are trying to find a rule or function which can predict almost the correct value of almost every x input.

So to achieve the same, cost function comes to safe –
So we need to find how well the hypothesis fit to the input data.

Suppose we have a training example –






The blue curve is the hypothesis value we derive from the cost function.
Now some cost function developed the 2nd drawing, red mark.

The purpose of the cost function is essentially fit the value of h(x).


From the above 2nd diagram it’s very clear that J1 is much better than J2 because J1 travelled through almost all the value of X, so predicted value of H(x) will naturally be better there. So J2 > J1.

·        Smaller the value of hypothesis the more better it is.
·        Main goal of machine learning is develop the value of h(x) so that the value of j remains the minimum.


So main characteristics of a cost function is
J(yi , h(xi))

It checks the observed response – yi
It try to find the value of predictive responses – h(xi)


"Gradient Descent is actually the method to minimize cost function of multiple variables"


Cost Function Example and Better Understanding

So we have something called hypothesis and other we have minimize function. So what these meant for.
So considering a popular example, let we have a minimize cost function-



No consider we have a dataset like following-



So for each value of X , the value of Y is as well same like (1:1 , 2:2 , 3:3)
So we have 3 datasets.
        ·        So consider we get Ɵ1 = 1.
So now my graph will exactly looking like the above as a straight line.


Here Ɵ value is expected as Ɵ1(x) = 1, then expected Y = 1 , but you need to subtract predicted value of y with actual value.







          ·        So consider we get Ɵ1 = 0.5



So the best possible value of Ɵ is 1 because then actual vs predicted value is same and that is what the cost function should derive.

If J(θ0,θ1)=0, that means the line defined by the equation "y=θ0+θ1x" perfectly fits all of our data












Feature Normalization Algorithm


 function [X_norm, mu, sigma] = featureNormalize(X)  
 %FEATURENORMALIZE Normalizes the features in X   
 %  FEATURENORMALIZE(X) returns a normalized version of X where  
 %  the mean value of each feature is 0 and the standard deviation  
 %  is 1. This is often a good preprocessing step to do when  
 %  working with learning algorithms.  
 % You need to set these values correctly  
 X_norm = X;  
 mu = zeros(1, size(X, 2));  
 sigma = zeros(1, size(X, 2));  
 % ====================== YOUR CODE HERE ======================  
 % Instructions: First, for each feature dimension, compute the mean  
 %        of the feature and subtract it from the dataset,  
 %        storing the mean value in mu. Next, compute the   
 %        standard deviation of each feature and divide  
 %        each feature by it's standard deviation, storing  
 %        the standard deviation in sigma.   
 %  
 %        Note that X is a matrix where each column is a   
 %        feature and each row is an example. You need   
 %        to perform the normalization separately for   
 %        each feature.   
 %  
 % Hint: You might find the 'mean' and 'std' functions useful.  
 %      
 features = size(X,2);  
 % the given values of mu and sigma as initial zeros  
 mu = zeros(1, features);  
 sigma = zeros(1, features);  
 %iterating through all the features, like we have 3 features follows-  
 % 1 2045 3  
 % 1 5678 2  
 for iter = 1 : features  
      %Mean mean(X..........)  
      mu(1, iter) = mean(X(:,iter));  
      X(:,iter) = X(:,iter) .- mu(1, iter);  
      %Now derive the following formula-  
      %Xi = (Xi – mean)/Sigma  
      %Standard Deviation can be made by just calling std(......)  
      sigma(1, iter) = std(X(:,iter));  
      X(:,iter) = X(:,iter) ./ sigma(1, iter);       
 end;  
      %display(X);  
      X_norm = X;       
 % ============================================================  
 end  


Gradient Descent with Multiple Feature Algorithm



 function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)  
 %GRADIENTDESCENTMULTI Performs gradient descent to learn theta  
 %  theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by  
 %  taking num_iters gradient steps with learning rate alpha  
 % Initialize some useful values  
 m = length(y); % number of training examples  
 J_history = zeros(num_iters, 1);  
 for iter = 1:num_iters  
   % ====================== YOUR CODE HERE ======================  
   % Instructions: Perform a single gradient step on the parameter vector  
   %        theta.   
   %  
   % Hint: While debugging, it can be useful to print out the values  
   %    of the cost function (computeCostMulti) and gradient here.  
   %  
      %Vectorization Fundamental  
           %h(theta)X = summ(theta(j) * X(j)); where j = 0 .. n  
               % = transpose(theta) * X;  
                      %where theta is a Vector  
                      %theta = [theta(1)/theta(2)/theta(3)] .. column Vector  
                      %X = [X(1)/X(2)/X(3)] ... column Vector  
      %Gradient Descent with Multiple Variables Formula  
      %theta(j) := theta(j) - alpha * 1/m * summ(h(theta)x(i) - y(i)).Xj(i)); where i = 0 ... n  
      %Xj(i) is Vector  
      %h(theta)x(i) = X*theta ... as it takes all the values  
      vectorMultiple = X' * (X*theta - y);  
      theta = theta - (alpha/m) * (vectorMultiple);  
      square = (X * theta - y).^2;  
      J_history(iter) = (1/(2*m))*sum(square);  
      %read more about vectorization  
      %https://www.gnu.org/software/octave/doc/interpreter/Basic-Vectorization.html  
   % ============================================================  
   % Save the cost J in every iteration    
   % J_history(iter) = computeCostMulti(X, y, theta);  
 end  
 end  


Gradient Descent for Single Feature Algorithm


Gradiant Descent Algorithm with Single Feature


 function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)  
 %GRADIENTDESCENT Performs gradient descent to learn theta  
 %  theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by   
 %  taking num_iters gradient steps with learning rate alpha  
 % Initialize some useful values  
 m = length(y); % number of training examples  
 J_history = zeros(num_iters, 1);  
 for iter = 1:num_iters  
   % ====================== YOUR CODE HERE ======================  
   % Instructions: Perform a single gradient step on the parameter vector  
   %        theta.   
   %  
   % Hint: While debugging, it can be useful to print out the values  
   %    of the cost function (computeCost) and gradient here.  
   %  
      % For J = 0 and J = 1, which should use the positions of theta 1 and 2 (indexes of octave)  
   % theta1 - alpha * 1/m * sum( theta0 * x(i) - y(i) ) for all i  
   % theta2 - alpha * 1/m * sum( theta1 * x(i) - y(i) ) * x(i) for all i  
      % defining the derivate of theta 1 portion  
      derivative1 = (1/m)*sum((X*theta) -y);       
      %Define the complete Equation for theta 1  
      temp1 = theta(1) - (alpha*derivative1);  
      % defining the derivate of theta 2 portion  
      derivative2 = (1/m)*sum(((X*theta) -y).* X(:,2));  
      %Define the complete Equation for theta 2  
      temp2 = theta(2) - (alpha*derivative2);  
      %Derive theta  
      theta = [temp1;temp2];  
      % In order to debug, the cost shows the decrease during the iterations.  
      % jCost = computeCost(X, y, theta)  
   %disp(jCost)  
   % ============================================================  
   % Save the cost J in every iteration    
   J_history(iter) = computeCost(X, y, theta);  
      %disp(J_history);  
 end  
 end  

Cost Function Algorithm in Matlab

computeCost(X, y , theta)


 function J = computeCost(X, y, theta)  
 %COMPUTECOST Compute cost for linear regression  
 %  J = COMPUTECOST(X, y, theta) computes the cost of using theta as the  
 %  parameter for linear regression to fit the data points in X and y  
 % Initialize some useful values  
 m = length(y); % number of training examples  
 % You need to return the following variables correctly   
 J = 0;  
 % ====================== YOUR CODE HERE ======================  
 % Instructions: Compute the cost of a particular choice of theta  
 %        You should set J to the cost.  
 prediction = X * theta;  
 square = (prediction - y).^2;  
 J = 1/(2*m)*sum(square);  
 % =========================================================================  
 end  



Monday, May 5, 2014

Hadoop Basics - A little behind Hadoop

     

This blog is not explaining anything about Development on hadoop, it actually explains about what hadoop and its basic components.

Some general points on Hadoop-

-Huge data needed to be processed and with variety and that’s the main thing about Hadoop
-Data stored in HDFS and HDFS corresponds to Cluster
-Process on these data happened by Map reduce and since data is already in the cluster hadoop emphasizes on inplace processing
-Consider Hive and Pig , both have their own way to present code , like query of latin statement, but both finally makes MR job and then that Run over hadoop cluster.
-HBase is a rela-time database run on top of HDFS
-Impala is used for low-latency query and directly runs on HDFS
-Some other hadoop ecosystem based tools are Flume , Hue , Ooize , Mahout etc.

 

HDFS

Consider we have text file of size 150 MB.

In HDFS (Hadoop Distributed File System) , these data will be saved in blocks.
Mostly each block is of 64 MB, so our data will distributed as follows-

   


Now each block will be saved in one of the nodes in the cluster as follows-



In each node of the cluster, there is a daemon all the time running is DATANODE.
When we need to know which blocks are handled by which node, that information is being handled by a daemon known as NAMENODE.
Information stored in Name Node is Meta-Data.


Problem with this System

There can be several problems with this system, few major-
·        Network Disk Failure , in this case nodes won’t be able to talk to each other
·        Disk failure of Data Node
·        Disk Failure on Name Node, and we can loose our meta-data.


Data Redundancy on HDFS

So to sort out the above problems, Hadoop maintains  2 copies of each data node and whenever any node fails , it automatically linked to the other copy , and Hadoop daemon is being instructed to make up the system again.




Name Node Failure

If name node is failure, it becomes a single point of failure and everything just stops and no meta data , no information and finally no job on data.
To ensure the high-Availability of Name node, a copy of name node is as well maintained which will ensures that name node will always be there and even if active name node is gone, there must be backup of name node .





MAP-REDUCE

If we have huge data, to process on a data in single thread may take long time and as well as we can run out of memory and those things can be solved by map-reduce job.

Hadoop doesn’t run on all data at once, but Hadoop believes in running on chunk of data parallel.

How Map-Reduce Actually Works

Consider a scenario where we need to make a map containing the sum of amounts spend by a credit card in different different cities.
To do this work , we have set of Map jobs and set of reducer jobs.





Now each mapper will take small amount of data to process and calculate the city and corresponding value on it. By the end each mapper will have pile of cards on them per city transaction.






So from above , blue 1 have highest chunk of NYC and MIAMI record but no records with LA but that won’t be same case with blue 2.
That’s the job of Mapper.
Now Job of Reducer starts
Now we have a set of reducers and we are going to assign some cities to each of the reducers.






Reducer 1 – NYC
Reducer 2 – MIAMI , LA







Now Reducer will collect only required piles of data directly from Mappers and that will be faster because Mapper already have a pile of data with it , so no further operation will be required.

Only thing reducer need to do is add all the amounts on their pile and that will give the total transaction happened in each city by Credit Card.

Mappers are little program which runs on little set of data and perform operation on it , known as intermediate data.
Hadoop works based on Key-value pair. After mapper operates on data , reducers get that data and work on each set of data from Map and process it and finally give the result.
Hadoop takes care of the Shuffle and Sort phase. You do not have to sort the keys in your reducer code, you get them in already sorted order.
In our case , Reducer has the key as City name and the process Reducer









JOB TRACKER

When we run a MR Job that is being submitted to Job Tracker. Job Tracker is responsible for splitting the job in different mappers and reducers depend upon the volume of data.

TASK TRACKER

Running the actual MR task actually on each node is actually done by a daemon Task Tracker.
Task Tracker runs on each of this node.
As Task Tracker runs on the same machine as the Data Node. So Hadoop framework helps to run the task on the same machine where the data resides and that help to reduce lot of network traffic.
So mapper works on the same data on the same machine but sometime the task tracker in the same nodes to be processed may be busy so in that case, the process will be handled by different node task tracker over network and that sometimes makes things slower.

Things we can do with Map-Reduce

Data processing
Log Analyzing
Big data queries
Machine Learning
Data Mining
Web Crawling
Item Classification
Fraud Detection



I have already setup Hadoop and lot more components on top of it.Mesos,Spark,Hive,Sqoop,Worked on Talend,YARN,PIG.I am currently working on more than 10 TB of data and working on setup a Big Data Infrastructure for Adhoc reporting and Analytics.



Friday, May 2, 2014

Java Web Application + Spring + Hibernate + Maven

Simple Java Web Application using DI & ORM framework on top of Maven


Problem Statement

Create a simple Java web application where a user can enter comments on a simple web page. The comments should be persisted to a MySQL database and displayed on the screen. The comments can be anonymous (no login/username required)


Database Script



 -- --------------------------------------------------------  
 -- Host:             127.0.0.1  
 -- Server version:        5.5.28 - MySQL Community Server (GPL)  
 -- Server OS:          Win64  
 -- HeidiSQL version:       7.0.0.4053  
 -- Date/time:          2014-04-29 21:18:39  
 -- --------------------------------------------------------  
 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;  
 /*!40101 SET NAMES utf8 */;  
 /*!40014 SET FOREIGN_KEY_CHECKS=0 */;  
 -- Dumping database structure for midas-test  
 CREATE DATABASE IF NOT EXISTS `midas-test` /*!40100 DEFAULT CHARACTER SET utf8 */;  
 USE `midas-test`;  
 -- Dumping structure for table midas-test.comments  
 CREATE TABLE IF NOT EXISTS `comments` (  
  `id` bigint(20) NOT NULL AUTO_INCREMENT,  
  `comment` mediumtext NOT NULL,  
  PRIMARY KEY (`id`)  
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
 -- Dumping data for table midas-test.comments: ~0 rows (approximately)  
 /*!40000 ALTER TABLE `comments` DISABLE KEYS */;  
 /*!40000 ALTER TABLE `comments` ENABLE KEYS */;  
 /*!40014 SET FOREIGN_KEY_CHECKS=1 */;  
 /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;  


Project Template

Create a Spring Project using Spring Tool or eclipse or maven.
The project structure will be like below-




















Controller Class


 import java.util.Locale;  
 import java.util.Map;  
 import org.slf4j.Logger;  
 import org.slf4j.LoggerFactory;  
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.stereotype.Controller;  
 import org.springframework.ui.Model;  
 import org.springframework.validation.BindingResult;  
 import org.springframework.validation.annotation.Validated;  
 import org.springframework.web.bind.annotation.ModelAttribute;  
 import org.springframework.web.bind.annotation.RequestMapping;  
 import org.springframework.web.bind.annotation.RequestMethod;  
 import com.test.comment.db.Comment;  
 import com.test.comment.model.CommentModel;  
 import com.test.comment.service.CommentService;  
 /**  
  * Handles requests for the application home page.  
  */  
 @Controller  
 public class HomeController {  
      private static final Logger logger = LoggerFactory  
                .getLogger(HomeController.class.getName());  
      @Autowired  
      private CommentService commentService;  
      // @Autowired  
      // private CommentValidator commentValidator;  
      /**  
       * Simply selects the home view to render by returning its name.  
       */  
      @RequestMapping(value = "/")  
      public String home(@ModelAttribute("comment") @Validated Comment cto,  
                Locale locale, Model model) {  
           return "views/login";  
      }  
      @RequestMapping(value = "/index", method = RequestMethod.GET)  
      public String loginPage(Locale locale, Model model) {  
           return "views/login";  
      }  
      @RequestMapping(value = "/user")  
      public String login(@ModelAttribute("comment") Comment cto,  
                @Validated CommentModel comments, Model model) {  
           return "views/user";  
      }  
      @RequestMapping("/list")  
      public String listComments(Map<String, Object> map) {  
           map.put("comment", new Comment());  
           map.put("commentList", commentService.listComment());  
           return "views/login";  
      }  
      @RequestMapping(value = "/add", method = RequestMethod.POST)  
      public String addComment(@ModelAttribute("comment") @Validated Comment cto,  
                BindingResult result, Model model) {  
           if (result.hasErrors()) {  
                logger.info("Returning empSave.jsp page");  
                return "";  
           }  
           String commentValue = cto.getCommentValue().trim();  
           if (commentValue != null && commentValue.length() > 0) {  
                commentService.addComment(cto);  
           }  
           return "redirect:/list";  
      }  
 }  

Spring Login page


 <%@ page language="java" contentType="text/html; charset=UTF-8"  
   pageEncoding="UTF-8"%>  
 <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>  
 <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
 <style>  
 .error {  
      color: #ff0000;  
 }  
 .errorblock {  
      color: #000;  
      background-color: #ffEEEE;  
      border: 3px solid #ff0000;  
      padding: 8px;  
      margin: 16px;  
 }  
 </style>  
 <title>Login Page</title>  
 </head>  
 <body>  
 <form:form method="post" action="add" commandName="comment">  
      <form:errors path="*" cssClass="errorblock" element="div" />  
      <tr>  
     <td><form:label path="commentValue"><spring:message code="label.commentValue"/></form:label></td>  
     <td><form:errors path="commentValue" cssClass="error" /></td>  
   </tr>   
   <tr>  
   <td><form:textarea path="commentValue" /></td>   
   </tr>  
   <tr>  
     <td colspan="2">  
     </td>  
   </tr>  
  <table>  
  </table><input type="submit" value='<spring:message code="label.addComment"/>'>  
 </form:form>  
 <h3><br><br><br><br><br><br>  LIST OF COMMENTS</h3>  
 <c:if test="${!empty commentList}">  
 <table class="data">  
 <tr>  
   <th>Comment</th>  
 </tr>  
 <c:forEach items="${commentList}" var="comment">  
   <tr>  
     <td>${comment.commentValue}</td>  
   </tr>  
 </c:forEach>  
 </table>  
 </c:if>  
 </body>  
 </html>  



Spring pom.xml


 <?xml version="1.0" encoding="UTF-8"?>  
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
      <modelVersion>4.0.0</modelVersion>  
      <groupId>com.journaldev</groupId>  
      <artifactId>CommentDemo</artifactId>  
      <name>CommentDemo</name>  
      <packaging>war</packaging>  
      <version>1</version>  
      <properties>  
           <java-version>1.7</java-version>  
           <org.springframework-version>4.0.3.RELEASE</org.springframework-version>  
           <org.springframework.version>3.2.0.RELEASE</org.springframework.version>  
           <org.aspectj-version>1.7.4</org.aspectj-version>  
           <org.slf4j-version>1.7.5</org.slf4j-version>  
           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
           <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
      </properties>  
      <dependencies>  
           <!-- Spring -->  
           <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-context</artifactId>  
                <version>${org.springframework-version}</version>  
                <exclusions>  
                     <!-- Exclude Commons Logging in favor of SLF4j -->  
                     <exclusion>  
                          <groupId>commons-logging</groupId>  
                          <artifactId>commons-logging</artifactId>  
                     </exclusion>  
                </exclusions>  
           </dependency>  
           <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-webmvc</artifactId>  
                <version>${org.springframework-version}</version>  
           </dependency>  
           <!-- AspectJ -->  
           <dependency>  
                <groupId>org.aspectj</groupId>  
                <artifactId>aspectjrt</artifactId>  
                <version>${org.aspectj-version}</version>  
           </dependency>  
           <!-- Logging -->  
           <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>slf4j-api</artifactId>  
                <version>${org.slf4j-version}</version>  
           </dependency>  
           <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>jcl-over-slf4j</artifactId>  
                <version>${org.slf4j-version}</version>  
                <scope>runtime</scope>  
           </dependency>  
           <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>slf4j-log4j12</artifactId>  
                <version>${org.slf4j-version}</version>  
                <scope>runtime</scope>  
           </dependency>  
           <dependency>  
                <groupId>log4j</groupId>  
                <artifactId>log4j</artifactId>  
                <version>1.2.15</version>  
                <exclusions>  
                     <exclusion>  
                          <groupId>javax.mail</groupId>  
                          <artifactId>mail</artifactId>  
                     </exclusion>  
                     <exclusion>  
                          <groupId>javax.jms</groupId>  
                          <artifactId>jms</artifactId>  
                     </exclusion>  
                     <exclusion>  
                          <groupId>com.sun.jdmk</groupId>  
                          <artifactId>jmxtools</artifactId>  
                     </exclusion>  
                     <exclusion>  
                          <groupId>com.sun.jmx</groupId>  
                          <artifactId>jmxri</artifactId>  
                     </exclusion>  
                </exclusions>  
                <scope>runtime</scope>  
           </dependency>  
           <!-- @Inject -->  
           <dependency>  
                <groupId>javax.inject</groupId>  
                <artifactId>javax.inject</artifactId>  
                <version>1</version>  
           </dependency>  
           <!-- Servlet -->  
           <dependency>  
                <groupId>javax.servlet</groupId>  
                <artifactId>servlet-api</artifactId>  
                <version>2.5</version>  
                <scope>provided</scope>  
           </dependency>  
           <dependency>  
                <groupId>javax.servlet.jsp</groupId>  
                <artifactId>jsp-api</artifactId>  
                <version>2.1</version>  
                <scope>provided</scope>  
           </dependency>  
           <dependency>  
                <groupId>javax.servlet</groupId>  
                <artifactId>jstl</artifactId>  
                <version>1.2</version>  
           </dependency>  
           <!-- HIBERNATE -->  
           <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-jdbc</artifactId>  
                <version>${org.springframework-version}</version>  
           </dependency>  
           <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-orm</artifactId>  
                <version>${org.springframework-version}</version>  
           </dependency>  
           <!-- Hibernate framework -->  
           <dependency>  
                <groupId>org.hibernate</groupId>  
                <artifactId>hibernate-core</artifactId>  
                <version>4.3.4.Final</version>  
           </dependency>  
           <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>slf4j-log4j12</artifactId>  
                <version>1.6.1</version>  
           </dependency>  
           <dependency>  
                <groupId>org.hibernate</groupId>  
                <artifactId>hibernate-c3p0</artifactId>  
                <version>4.3.4.Final</version>  
           </dependency>  
           <dependency>  
                <groupId>org.hibernate</groupId>  
                <artifactId>hibernate-entitymanager</artifactId>  
                <version>4.3.4.Final</version>  
           </dependency>  
           <!-- <dependency>  
                <groupId>org.hibernate</groupId>  
                <artifactId>hibernate-annotations</artifactId>  
                <version>3.5.6-Final</version>  
           </dependency>  
           <dependency>  
                <groupId>org.hibernate</groupId>  
                <artifactId>hibernate-commons-annotations</artifactId>  
                <version>3.2.0.Final</version>  
           </dependency>  
  -->  
           <dependency>  
                <groupId>org.hibernate</groupId>  
                <artifactId>hibernate-ehcache</artifactId>  
                <version>4.3.4.Final</version>  
           </dependency>  
           <!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId>   
                <version>5.1.30</version> </dependency> <dependency> <groupId>commons-dbcp</groupId>   
                <artifactId>commons-dbcp</artifactId> <version>20030825.184428</version>   
                </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId>   
                <version>20030825.183949</version> </dependency> -->  
           <dependency>  
                <groupId>mysql</groupId>  
                <artifactId>mysql-connector-java</artifactId>  
                <version>5.1.9</version>  
           </dependency>  
           <dependency>  
                <groupId>dom4j</groupId>  
                <artifactId>dom4j</artifactId>  
                <version>1.6.1</version>  
           </dependency>  
           <dependency>  
                <groupId>commons-logging</groupId>  
                <artifactId>commons-logging</artifactId>  
                <version>1.1.1</version>  
           </dependency>  
           <dependency>  
                <groupId>commons-collections</groupId>  
                <artifactId>commons-collections</artifactId>  
                <version>3.2.1</version>  
           </dependency>  
           <dependency>  
                <groupId>antlr</groupId>  
                <artifactId>antlr</artifactId>  
                <version>2.7.7</version>  
           </dependency>  
           <!-- Test -->  
           <dependency>  
                <groupId>junit</groupId>  
                <artifactId>junit</artifactId>  
                <version>4.7</version>  
                <scope>test</scope>  
           </dependency>  
      <!--      <dependency>  
                <groupId>javax.validation</groupId>  
                <artifactId>validation-api</artifactId>  
                <version>1.1.0.Final</version>  
           </dependency> -->  
      </dependencies>  
      <build>  
           <plugins>  
                <plugin>  
                     <artifactId>maven-eclipse-plugin</artifactId>  
                     <version>2.9</version>  
                     <configuration>  
                          <additionalProjectnatures>  
                               <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>  
                          </additionalProjectnatures>  
                          <additionalBuildcommands>  
                               <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>  
                          </additionalBuildcommands>  
                          <downloadSources>true</downloadSources>  
                          <downloadJavadocs>true</downloadJavadocs>  
                     </configuration>  
                </plugin>  
                <plugin>  
                     <artifactId>maven-resources-plugin</artifactId>  
                     <version>2.5</version>  
                     <configuration>  
                          <encoding>UTF-8</encoding>  
                     </configuration>  
                </plugin>  
                <plugin>  
                     <artifactId>maven-compiler-plugin</artifactId>  
                     <version>3.0</version>  
                     <configuration>  
                          <source>${java-version}</source>  
                          <target>${java-version}</target>  
                          <compilerVersion>${java.version}</compilerVersion>  
                          <encoding>UTF-8</encoding>  
                     </configuration>  
                </plugin>  
                <plugin>  
                     <groupId>org.codehaus.mojo</groupId>  
                     <artifactId>exec-maven-plugin</artifactId>  
                     <version>1.2.1</version>  
                     <configuration>  
                          <mainClass>org.test.int1.Main</mainClass>  
                     </configuration>  
                </plugin>  
           </plugins>  
      </build>  
 </project>  

make sure the version of Spring , hibernate must be compatiable.
One point to take care , if using Spring 3 , use Hibernate 3 only same in case of Spring 4.


Application Context File

 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
      <!-- The definition of the Root Spring Container shared by all Servlets   
           and Filters -->  
      <context-param>  
           <param-name>contextConfigLocation</param-name>  
           <param-value>/WEB-INF/spring/root-context.xml</param-value>  
      </context-param>  
      <!-- Creates the Spring Container shared by all Servlets and Filters -->  
      <listener>  
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
      </listener>  
      <!-- Processes application requests -->  
      <servlet>  
           <servlet-name>appServlet</servlet-name>  
           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
           <init-param>  
                <param-name>contextConfigLocation</param-name>  
                <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>  
           </init-param>  
           <load-on-startup>1</load-on-startup>  
      </servlet>  
      <servlet-mapping>  
           <servlet-name>appServlet</servlet-name>  
           <url-pattern>/</url-pattern>  
      </servlet-mapping>  
      <filter>  
    <filter-name>hibernateFilter</filter-name>  
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
    <init-param>  
      <param-name>sessionFactoryBeanName</param-name>  
      <param-value>sessionFactory</param-value>       
    </init-param>     
   </filter>  
 </web-app>  


Database Settings

Run the DB Script mentioned above

JDBC Properties-
 jdbc.driverClassName=com.mysql.jdbc.Driver  
 jdbc.dialect=org.hibernate.dialect.MySQLDialect  
 jdbc.url=jdbc:mysql://localhost:3306/midas-test  
 jdbc.username=root  
 jdbc.password=root  


HBM File
 <?xml version='1.0' encoding='utf-8'?>  
 <!DOCTYPE hibernate-configuration PUBLIC  
   "-//Hibernate/Hibernate Configuration DTD//EN"  
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
 <hibernate-configuration>  
   <session-factory>  
     <mapping class="com.test.comment.db.Comment" />  
   </session-factory>  
 </hibernate-configuration>  


Rest all the files are Hibernate DAO , Service file.
Download the zip to test the project.
Maven Compile and package the same before using it.
Download Project