-->
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.  [ 1 post ] 
Author Message
 Post subject: NullPointerException @ session.beginTransaction();
PostPosted: Sat Dec 04, 2010 6:10 am 
Newbie

Joined: Sat Dec 04, 2010 5:58 am
Posts: 2
Hi all,

I am new to Hibernate. I wrote a small program to save an object into the oracle database. I am getting NullPointerException in the following

tx = session.begintransaction

Please find my code

My POJO class
===========

import java.util.Date;

public class Employee
{
private int empno;
private int deptno;
private String ename;
private String job;
private double sal;
private Date hiredate;

// Getters and Setters method
}

=================================================================================
My hbm file

<?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 package="com.saravanan.hibernate.domain">
<class name="Employee" table="emp" lazy="false">

<id name="empno" type="int">
<column name="empno" />
<generator class = "increment" />
</id>

<property name="ename" column="ename" />
<property name="sal" />
<property name="job" />
<property name="hiredate" />
<property name="deptno" />
</class>

</hibernate-mapping>

=====================================================================================

My mapping file

<?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 = "connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name = "connection.url">jdbc:oracle:thin:@localhost:1030:GDN</property>
<property name = "connection.user">scott</property>
<property name = "connection.password">tiger</property>
<property name = "dialect">org.hibernate.dialect.OracleDialect</property>

<mapping resource = "com/saravanan/hibernate/mappings/Employee.hbm.xml" />

</session-factory>
</hibernate-configuration>

=====================================================================================

My test class

import org.hibernate.cfg.*;
import org.hibernate.*;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import java.util.Date;

import com.saravanan.hibernate.domain.Employee;

public class HibernateBasicTestCase
{

private SessionFactory factory;

/**
* @param args
*/
public static void main(String[] args)
{
//Logger
HibernateBasicTestCase test = new HibernateBasicTestCase();

//Preparing Configuration object
System.out.println( "Preparing configuration object..." );
Configuration cfg = new Configuration();
cfg.configure();

//Building SessionFactory
System.out.println( "Building SessionFactory..." );
test.factory = cfg.buildSessionFactory();

System.out.println( "Building Persistant Object..." );
Employee emp = new Employee();
emp.setEname("SARAVANAN");
emp.setSal( 20000 );
emp.setJob("ASE");
emp.setHiredate( new Date() );
emp.setDeptno(20);

test.createEmployee(emp);

//updating

}

public void createEmployee( Employee emp )
{
Session session = null;
Transaction tx = null;

try
{
session = factory.openSession();
System.out.println( "Begining transaction for Saving Employee object...." );
if ( session != null )
System.out.println ("session is not null" );
tx = session.beginTransaction();

System.out.println( "Saving Employee object...." );
session.save( emp ); //Save new Employee object
tx.commit();
System.out.println( "Employee created with empno: " + emp.getEmpno() );
}
catch( HibernateException hexp )
{
tx.rollback();
System.out.println( "Exception in creating an Employee" );
}
finally
{
session.close();
}
}
}

=====================================================================================

############## My output in Eclipse #############################

Preparing configuration object...
log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Building SessionFactory...
Building Persistant Object...
Begining transaction for Saving Employee object....
session is not null
Exception in thread "main" java.lang.NullPointerException
at com.saravanan.hibernate.test.HibernateBasicTestCase.createEmployee(HibernateBasicTestCase.java:67)
at com.saravanan.hibernate.test.HibernateBasicTestCase.main(HibernateBasicTestCase.java:41)


Kindly advise....


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

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.