in Education by
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)

1 Answer

0 votes
by
You need not have transaction for this. Since you just want to read from the table and not write into the table.

Related questions

0 votes
    My problem is the same as described in [1] or [2]. I need to manually set a by default auto- ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have text stored in a database table, many short rows about 70-90 characters long fields. (of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I have text stored in a database table, many short rows about 70-90 characters long fields. (of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am looking for a free tool to load Excel data sheet into an Oracle database. I tried the Oracle ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 22, 2022 in Education by JackTerrance
0 votes
    I am looking for a free tool to load Excel data sheet into an Oracle database. I tried the Oracle ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    What is BLOB data type in Oracle?...
asked Dec 18, 2020 in Technology by JackTerrance
0 votes
    Facing issue in session with the upgrade of my application to Spring 4.1.9 and Hibernate 4.3.11.. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I am new to spring-maven-hibernate and i cannot solve this following error here is my console output. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I created update() method which is using JPA. It looks like this: public boolean update(Programy program) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    I am wanting to understand this Karaf JPA example. When I follow the instructions to add features and run ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    Which of the following is not a core interface of Hibernate? (a) Configuration (b) Criteria (c) ... Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of the following is not a state of object in Hibernate? (a) Attached() (b) Detached() (c) ... Autoboxing & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of the following is not an advantage of using Hibernate Query Language? (a) Database independent (b) ... Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    I have a problem with Hibernate. In Short: How to configure a ManyToMany association with Hibernate when the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    @Entity class A { @ManyToMany private List list; ... } @Entity class B { ... } I'd like to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
...