2011-12-10 42 views
0

我正在學習hibernate。我創建了一個簡單的Web應用程序,使用休眠模式從Oracle 11g獲取數據。在休眠狀態下獲取oracle數據

我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.dialect">org.hibernate.dialect.OracleDialect</property> 
    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property> 
    <property name="hibernate.connection.username">hr</property> 
    <property name="hibernate.connection.password">titu</property> 
    <property name="hibernate.connection.pool_size">1</property> 
    <mapping class="Course" package="com.vaannila.course.Course.java" resource="com/vaannila/course/Course.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

我有一個表「課程」表中的數據庫,從我想要得到的數據。爲此,我創建了一個映射XML文件和POJO文件。

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"> 
<hibernate-mapping> 
    <class name="com.vaannila.course.Course" table="COURSES"> 
     <meta attribute="class-description"> 
      This class contains the course details. 
     </meta> 
     <id name="courseId" column="COURSE_ID" type="integer"/> 
     <property name="courseName" column="COURSE_NAME" type="string" not-null="true"/> 
    </class> 
</hibernate-mapping> 

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; 
    } 
} 

我希望得到一個jsp頁面這是對數據如下:

<%-- 
    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> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <% 

     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(); 
     } 
     %> 
    </body> 
</html> 

我已經創建了一個用於創建會話工廠對象的「HibernateUtil」類:

/* 
* 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; 
    } 
} 

當我在tomcat服務器上運行這個項目時,它給出了空指針異常。當我調試我的項目,然後我發現

transaction= db_session.beginTransaction(); 

jsp中的這條線給我空。意思是beginTransaction()返回null。我不知道爲什麼是這樣?

請提前告訴我,在此先感謝

+0

你確定嗎?交易是無效的開始。你走了嗎? – greyfairer

回答

0

您無需爲此進行交易。既然你只是想從表中讀取而不寫入表格。