JaiswalTraining

Get the online training

Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







ORM Module

The ORM package provides integration layers for popular object-relational mapping APIs, including JPA, JDO, Hibernate, and iBatis.

Spring-Hibernate Integration

  • We can Integrate other framework with Spring.
  • Required  Spring-configuration xml file
  • There are 3 ways to use hibernate with Spring
First Way:-


<!-- 1st Way -->
  <bean id="hibernatesessionfactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation"
value=
"/WEB-INF/classes/myutility/HibernateConfiguration.cfg.xml"/>
  </bean>

LocalSessionFactoryBean
is a Spring factory bean that produces a local Hibernate SessionFactory instance3 that draws its mapping metadata from one or more XML mapping files.




Hibernate.cfg.xml


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">
oracle.jdbc.driver.OracleDriver</property>

    <property name="hibernate.connection.url">
jdbc:oracle:thin:
@localhost:1521:XE</property>
    <property name="hibernate.connection.username">system</property>
    <property name="hibernate.connection.password">manager</property>

    <property name="hibernate.dialect">
org.hibernate.dialect.OracleDialect</property>


    <property name="hibernate.show_sql">true</property>

    <mapping resource="myutility/HibernateMapping.hbm.xml" />
  </session-factory>
</hibernate-configuration>


Second Way:-


<bean id="hibernatesessionfactory"
class
="org.springframework.orm.hibernate3.
LocalSessionFactoryBean"
>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.connection.driver_class">
oracle.jdbc.driver.OracleDriver</prop>
          <prop key="hibernate.connection.url">
jdbc:oracle:thin:@localhost:1521:XE</prop>
          <prop key="hibernate.connection.username">
system</prop>

          <prop key="hibernate.connection.password">
manager</prop>

        <prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect</prop>

      </props>
    </property>
    <property name="mappingResources">
      <list>
        <value>myutility/HibernateMapping.hbm.xml</value>
      </list>
    </property>
  </bean>

Third Way:-



<bean id="springdatasource"
class
="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="
oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="
jdbc:oracle:thin:@localhost:1521:XE"/>
    <property name="username" value="system"/>
    <property name="password" value="manager"/>
  </bean>
  
  <bean id="hibernatesessionfactory"
 
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="springdatasource"/>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect</prop>

      </props>
    </property>
    <property name="mappingResources">
      <list>
        <value>myutility/HibernateMapping.hbm.xml</value>
      </list>
    </property>
  </bean>

  • The dataSource property refers to any implementation of javax.sql.DataSource.
  • The mappingResources property takes a list of one or more paths to mapping files as resources in the
    classpath..

Example:- In this example we are agian fetching the students' records..!!!

welcome2.jsp



<h2><font color="green"></>Welcome to Student Database</font></h2></br>
</br>
<a href="./ShowRecords2">Show the Records</a>

ShowRecords2.java



package myservlet;

import java.util.List;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.
context.WebApplicationContext;

import org.springframework.web
.context.support.WebApplicationContextUtils;


import mybean.JavaBean;
import mypack.Student;

public class ShowRecords extends HttpServlet {

  protected void doGet(HttpServletRequest request,
      HttpServletResponse response)
throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    Student stud = new Student();
    ServletContext stcx = getServletContext();
    WebApplicationContext springcontainer =
WebApplicationContextUtils

        .getRequiredWebApplicationContext(stcx);
    JavaBean javabean = (JavaBeanspringcontainer.
getBean
("javabean");

    List list = javabean.searchStudent(stud);
    ArrayList arrlist = new ArrayList(list);
    Iterator iterator = arrlist.iterator();
    while (iterator.hasNext()) {
      stud = (Studentiterator.next();
      out.print(stud.getRoll() "------ " +
 stud.getName
() "</br>");
      out.print("</br>");

    }

  }

}

JavaBean.java



package mybean;

import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;

import mypack.Student;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class JavaBean {

  private SessionFactory sessionfactory;

  public void setSessionfactory(SessionFactory sessionfactory) {
    this.sessionfactory = sessionfactory;
  }

  public List searchStudent(Student stud) {
    Session session = null;
    List list = null;
    try {
      session = sessionfactory.openSession();

      String hql = "from Student";
      Query query = session.createQuery(hql);
      list = query.list();

    catch (HibernateException he) {
      he.printStackTrace();
      stud = null;
    }
    return list;
  }

}


Student.java



package mypack;

public class Student {
  private int roll;
  private String name;

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

  public void setRoll(int roll) {
    this.roll = roll;
  }

  public String getName() {
    return name;
  }

  public int getRoll() {
    return roll;
  }

}


Hibernateconfiguration.cfg.xml


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
 
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">
oracle.jdbc.driver.OracleDriver</property>

      <property name="hibernate.connection.url">
jdbc:oracle:thin:
@localhost:1521:XE</property>
      <property name="hibernate.connection.username">
system</property>

      <property name="hibernate.connection.password">
manager</property>

      
      <property name="hibernate.dialect">
org.hibernate.dialect.OracleDialect</property>

      
      <property name="hibernate.show_sql">
true</property>
      
      <mapping resource="myutility/HibernateMapping.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

HibernateMapping.hbm.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<hibernate-mapping>
  <class name="mypack.Student" table="stud">
    <id name="roll">
      <column name="roll"/>
      <generator class="assigned"/>
    </id>
    <property name="name">
      <column name="name"/>
    </property>
    
  </class>
</hibernate-mapping>

springconfiguration.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.
org/schema/beans http://www.springframework.org/
schema/beans/spring-beans-2.5.xsd"
>

 
  <bean id="hibernatesessionfactory"
 
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation"
value=
"/WEB-INF/classes/myutility/HibernateConfiguration.cfg.xml"/>
  </bean>
 
 <bean id="javabean" class="mybean.JavaBean">
    <property name="sessionfactory" ref="hibernatesessionfactory"/>
  </bean> 

  
</beans>

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id=
"WebApp_ID" version="2.5">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/SpringConfiguration.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.
ContextLoaderListener</listener-
class>
  </listener>
  <welcome-file-list>
    <welcome-file>welcome2.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>ShowRecords</display-name>
    <servlet-name>ShowRecords</servlet-name>
    <servlet-class>myservlet.ShowRecords</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ShowRecords</servlet-name>
    <url-pattern>/ShowRecords2</url-pattern>
  </servlet-mapping>
</web-app>


Output:-







Download Zip Code