-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: NullPointerException at session.beginTransaction();
PostPosted: Thu Oct 29, 2009 11:57 am 
Newbie

Joined: Thu Oct 29, 2009 11:21 am
Posts: 2
I am just stepping into Hibernate. I tried my first example provided at http://www.tutcity.com/view/hibernate-example-using-hibernate-tools.19730.html

My configuration file is
Code:
<?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.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">pwd</property>
        <property name="hibernate.connection.url">http://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping resource="com/vaannila/course/Course.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


The mapping file is
Code:
<?xml version="1.0"?>
<!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 represents a row in the COURSES table
      </meta>
      <id name="courseId" type="long" column="COURSE_ID">
         <generator class="native"></generator>
      </id>
      <property name="courseName" type="string" column="COURSE_NAME"
         not-null="true" />
   </class>
</hibernate-mapping>


And my main mthod is
Code:
package com.vaannila.course;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.vaannila.util.HibernateUtil;

public class Main {

   /**
    * @param args
    */
   public static void main(String[] args) {
      Main obj = new Main();
      Long courseId1 = obj.saveCourse("Physics");
      Long courseId2 = obj.saveCourse("Chemistry");
      Long courseId3 = obj.saveCourse("Maths");
   }
   
   public Long saveCourse(String courseName)
   {
                          // gets the session
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction transaction = null;
      Long courseId = null;
      try {
         transaction = session.beginTransaction();
         Course course = new Course();
         course.setCourseName(courseName);
         courseId = (Long) session.save(course);
         transaction.commit();
      } catch (HibernateException e) {
         transaction.rollback();
         e.printStackTrace();
      } finally {
         session.close();
      }
      return courseId;
   }

}


When i run this code, i get a NullPointerException at the point transaction = session.beginTransaction(); eventhough session is not null.

What could be the problem?

I am using MySQL.


Top
 Profile  
 
 Post subject: Re: NullPointerException at session.beginTransaction();
PostPosted: Fri Oct 30, 2009 1:07 am 
Newbie

Joined: Thu Oct 29, 2009 11:21 am
Posts: 2
The URL for my DB was invalid.... just noticed it :-)


Top
 Profile  
 
 Post subject: Re: NullPointerException at session.beginTransaction();
PostPosted: Fri Oct 30, 2009 2:52 am 
Newbie

Joined: Fri Oct 30, 2009 2:48 am
Posts: 1
Hi,

me too learning hibernate with the same example..but still getting the same session.beginTransacion() null error. could you pls post ur mysql table structure...

i have created the COURSES table in mySQL like this,

CREATE TABLE COURSES (
COURSE_ID BIGINT PRIMARY KEY,
COURSE_NAME VARCHAR(100)
);

Help me...also post ur configuration file for connection url..thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.