i am learning hibernate. i have created a simple web application in which i am getting data from oracle 11g using hibernate.
My hibernate.cfg.xml file is :
<?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">
org.hibernate.dialect.OracleDialect
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@localhost:1521:ORCL
hr
titu
1
I have a table "Courses" table in database, from i want to get data. For this i have created a mapping XML file and POJO file.
Course.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">
This class contains the course details.
Course.java :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.vaannila.course;
/**
*
* @author titu
*/
import org.hibernate.*;
public class Course {
int courseId ;
String courseName;
public Course() {}
public Course(String courseName) {
this.courseName = courseName;
}
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
}
I want to get data on a jsp page which is as follows :
<%--
Document : index
Created on : Dec 9, 2011, 10:07:21 PM
Author : titu
--%>
<%@page import="com.vaannila.course.Course"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="com.vaannila.common.HibernateUtil"%>
<%@page import="org.hibernate.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
JSP Page
<%
Session db_session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction= null;
Integer courseId= null;
try{
if(db_session==null)
{
out.println("This is null");
}
transaction= db_session.beginTransaction();
List courses= db_session.createQuery("from Course").list();
for(Iterator iterator= courses.iterator(); iterator.hasNext();)
{
Course course= (Course) iterator.next();
out.println(course.getCourseName());
}
transaction.commit();
}
catch(HibernateException e){
transaction.rollback();
e.printStackTrace();
}
finally{
db_session.close();
}
%>
I have create a "HibernateUtil" class for creating session factory object :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.vaannila.common;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
*
* @author titu
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static{
try{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(Throwable ex)
{
System.err.println("Initial session factory creation failed " + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
When i am running this project in tomcat server then, it is giving null pointer exception. When i debug my project then i found that
transaction= db_session.beginTransaction();
this line in jsp is giving me null. Means beginTransaction() returns null. I dont know why it is so ?
Please suggest me , Thanks in advance
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)