Srikanth Technologies

Songs Project using Web, DAO and Spring + Hibernate

This is another application to store details of songs using a Web application that uses Hibernate through Spring Core. This application is same as the previous one, Songs With Spring + JDBC, in terms of Database and View, and Spring components, but Spring beans use Hibernate instead of Jdbc to talk to database.

Create Web Application

Use NetBeans (or any other IDE of your choice) to create a new web application. It uses Tomcat. Again you can choose whatever server you like.

I am naming the project as SongsSpringHibernate as we us Spring to access database through Hibernate.

Add Spring Framework 3.0 library, Hibernate Library, and JSTL library to project. Also include OJDBC14.jar to libraries as it contains Jdbc driver for Oracle.

In order to use AOP with Spring (we use AOP for transaction management), you need to add the following .jar files:

I have made all these files available as a single download at http://www.megafileupload.com/en/file/311042/springdeplib-rar.html. So download and add these .jar files to your project libraries so that they are placed in the classpath.

Add the following files from one of the previous projects, Songs With Hibernate, as they remain the same for this project also.

spring/songs.xml

This is configuration file for Spring beans.

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    <bean id="oracledatasource" 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="music" />
        <property name="password" value="music" />
    </bean>
    
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="oracledatasource"/>
        <property name="mappingResources">
            <list>
                <value>song.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                    hibernate.dialect=org.hibernate.dialect.OracleDialect
            </value>
        </property>
    </bean>
    <bean id="songsbean" class="spring.SongsManagement">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>
 </beans>
 

spring/SongsManagement.java

This is a bean in Spring that manages songs. It extends HibernateDaoSupport and uses Hibernate Template(obtained by getHibernateTemplate()) to access database through Hibernate.
package spring;

import entities.Song;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class SongsManagement extends HibernateDaoSupport {
    public void addSong(String title, String singer ){
          Song s = new Song();
          s.setTitle(title);
          s.setSinger(singer);
          this.getHibernateTemplate().save(s);
    }
    public List getSongs() {
        return  (List) this.getHibernateTemplate().find("from Song");
    }
}

That's All

As all other files remain the same, just deploy the project and run index.jsp.