-->
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.  [ 7 posts ] 
Author Message
 Post subject: NullPointerException at session.save(obj)
PostPosted: Thu Nov 17, 2005 3:39 am 
Newbie

Joined: Thu Nov 17, 2005 3:21 am
Posts: 2
Hi,
I am trying to run a sample stand alone hibernate program. I am using hibernate3 with MySql database. I am getting the following exception when run.

[i]java.lang.NullPointerException
at org.hibernate.jdbc.AbstractBatcher.closeConnection(AbstractBatcher.ja
va:505)
at org.hibernate.engine.TransactionHelper.doWorkInNewTransaction(Transac
tionHelper.java:142)
at org.hibernate.id.TableGenerator.generate(TableGenerator.java:94)
at org.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:
61)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId
(AbstractSaveEventListener.java:91)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGene
ratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrR
equestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTran
sient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(
DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpda
te(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:524)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:510)
at Test.main(Test.java:17)[/i]

It says the problem is with line 17 in Test.java
Test.java is below

[code]public class Test
{

public static void main(String args[])
{
try
{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
CD cd = new CD();
cd.setId(1);cd.setTitle("Ravi");cd.setArtist("Kumar");cd.setPurchasedate(new Date());cd.setCost(4.33);
session.save(cd); // [b]problem here[/b]
session.flush();
session.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}[/code]

The mapping file is

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

[code]<hibernate-mapping>
<class name="CD" table="cd">
<id name="id" type="int">
<column name="ID"/>
<generator class="hilo"/>
</id>
<property name="title"/>
<property name="artist"/>
<property name="purchasedate" type="date"/>
<property name="cost" type="double"/>
</class>
</hibernate-mapping>[/code]

Please guide how to get rid of this exception.

Thanks
Ravi Kumar

[/i]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 4:10 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
Does

Code:
Transaction tx = session.beginTransaction();


and a later commt help ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 4:12 am 
Beginner
Beginner

Joined: Sat Oct 22, 2005 11:16 pm
Posts: 40
You don't have any open JDBC connections!

For a simple testing standalone app like this, you need to have some code BEFORE the sessionFactory.openSession() which gets you a JDBC connection. Look up JDBC to see how to get a JDBC connection object for your database. Then you can do sessionFactory.openSession(connection). Then it will work. Remember to do connection.close() after you do session.close().


Top
 Profile  
 
 Post subject: No need of jdbc connection
PostPosted: Thu Nov 17, 2005 4:56 am 
Newbie

Joined: Thu Nov 17, 2005 3:21 am
Posts: 2
I have never seen any hibernate application needs jdbc connection.
And opening a transation and commiting is also optional. There must me some thing else as root of the problem. Please anybody try to help me. I am struggling since yesterday...


Thanks
Ravi Kumar


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 6:38 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
You are using the wrong dtd in your mapping:

Quote:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping
DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">


If you are really using hibernate3 it should
be like:

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">


And can you post your hibernate.cfg.xml ?


Top
 Profile  
 
 Post subject: Re: No need of jdbc connection
PostPosted: Thu Nov 17, 2005 1:44 pm 
Beginner
Beginner

Joined: Sat Oct 22, 2005 11:16 pm
Posts: 40
ravi_kumar wrote:
I have never seen any hibernate application needs jdbc connection.
And opening a transation and commiting is also optional. There must me some thing else as root of the problem. Please anybody try to help me. I am struggling since yesterday...


Look at the full source code. Where is the JDBC connection coming from in this case?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 4:17 pm 
Newbie

Joined: Fri Oct 28, 2005 10:54 pm
Posts: 12
What does your hibernate.properties or hibernate.cfg.xml look like. I am using Hibernate 2 and includes properties like the following for MySQL

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.password=password
hibernate.connection.url=jdbc\:mysql\://127.0.0.1\:3306/test
hibernate.connection.username=root
hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect
hibernate.show_sql=true


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.