Thursday 14 July 2016

Create the Hibernate Application in Eclipse IDE

We follow following steps to create hibernate application :

  1. Create the Dynamic web project
  2. Add jar files for hibernate
  3. Create the Persistent class
  4. Create the mapping file for Persistent class(.hbm.xml)
  5. Create the Configuration file(.cfg.xml)
  6. Create the class that retrieves or stores the persistent object
  7. Run the application

Step 1: Create the project(New-->Dynamic web project)


Step 2: Add required jar files























Step 3: Create the Persistent Class

Testing.java


package com.java;
public class Testing {
private String name,email,password;
private int amt;
public String getName() {return name;}

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

public String getEmail() {return email;}

public void setEmail(String email) {this.email = email;}

public String getPassword() {return password;}

public void setPassword(String password) {this.password = password;}

public int getAmt() {return amt;}

public void setAmt(int amt) {this.amt = amt;}}


Step 4:Create the mapping and configuration file for persistent class


src-->New-->other--Hibernate-->console configuration file




i)Select Database connection and select the server what we used.here we used MYSQL.Select mysql server 
ii)Select Configuration file and create new





hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
         <mapping resource="mypack/testing.hbm.xml"/>
    </session-factory>
</hibernate-configuration>



Step 5: Create Mapping file

Testing.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 14, 2016 3:37:00 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.java.Testing" table="TESTING">
        <id name="name" type="java.lang.String">
            <column name="Name" />
            <generator class="assigned" />
        </id>
        <property name="email" type="java.lang.String">
            <column name="email" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" />
        </property>
        <property name="amt" type="int">
            <column name="Amount" />
        </property>
    </class>
</hibernate-mapping>

Step 6: Create the servlet class for store and retrive the persistent object


home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<form action="New">
NAME<input type="text" name="name"/>
PASSWORD<input type="password" name="password"/>
EMAIL<input type="text" name="email"/>
Amount<input type="text" name="amt"/>
<input type="submit" value="Login">
</form>
</body>
</html>


New.java


package com.jeni;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class New extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
       
        String name=request.getParameter("name");
        String email=request.getParameter("email");
        String password=request.getParameter("password");
        String amt1=request.getParameter("amt");
        int amt=Integer.parseInt(amt1);
       
        //creating configuration object  
            Configuration cfg=new Configuration();  
            cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file  
              
            //creating session factory object  
            SessionFactory factory=cfg.buildSessionFactory();  
              
            //creating session object  
            Session session=factory.openSession();  
              
            //creating transaction object  
            Transaction t=session.beginTransaction();  
                  
            Testing e1=new Testing();  
            e1.setName(name);
            e1.setPassword(password);
            e1.setEmail(email); 
            e1.setAmt(amt);
            
            session.persist(e1);//persisting the object  
              
            t.commit();//transaction is committed  
            session.close();  
            out.println("successfully saved");  
             }
        catch(Exception e)
        {
        System.out.println(e);
        }}}

    web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>DynamicH</display-name>
  <servlet>
        <servlet-name>New</servlet-name>
        <servlet-class>com.jeni.New</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>New</servlet-name>
        <url-pattern>/New</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
  <welcome-file-list>
     <welcome-file>home.jsp</welcome-file>
  </welcome-file-list>
</web-app>


//Same as persistent class name,hbm file name and database table 






No comments:

Post a Comment