【问题标题】:Getting oracle data in hibernate在休眠中获取oracle数据
【发布时间】:2011-12-19 16:46:42
【问题描述】:

我正在学习休眠。我创建了一个简单的 Web 应用程序,我在其中使用休眠从 oracle 11g 获取数据。

我的 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 文件。

课程.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>

课程.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 中的这一行给了我 null。表示 beginTransaction() 返回 null。不知道为什么会这样?

请给我建议,提前谢谢

【问题讨论】:

  • 你确定吗?交易开始时为空。你踩过去了吗?

标签: hibernate


【解决方案1】:

您不需要为此进行交易。由于您只想从表中读取而不是写入表中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 2016-07-24
    • 2012-08-14
    • 1970-01-01
    相关资源
    最近更新 更多