aspose file tools*
The moose likes Object Relational Mapping and the fly likes sping+hibernate got NullException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "sping+hibernate got NullException" Watch "sping+hibernate got NullException" New topic
Author

sping+hibernate got NullException

YanJun Tong
Greenhorn

Joined: Jun 11, 2003
Posts: 27
Hi all,I got a problem in my application which uses spring and hibernate
the code is as follows:

List list=this.getHibernateTemplate().find( "from AccdentsVO o where o.accidentsID=?", new Integer(1));
I got a "java.lang.NullException" in the above line.
The AccidentsVO class is a javabean with a couple of getter and setter methods for examle:setAccidentsID(int i);int getAccidentsID() Furthermore I am sure there is a record in my accident table whose accidentsID is 1.It does work when I use getHibernateTemplate().save(...) I am not very clear why this time I can not use the find(...) method in spring framework.
Regards
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by YanJun Tong:
List list=this.getHibernateTemplate().find( "from AccdentsVO o where o.accidentsID=?", new Integer(1));

Did the above line cause the NullPointerException (first line in stack trace)? If so, getHibernateTemplate() must be returning null for this cannot be null. If not, you'll need to include the actual stack trace for anyone to be able to help you.
YanJun Tong
Greenhorn

Joined: Jun 11, 2003
Posts: 27
Hi it seems like the this.getHibernateTemplate().find(...)causes the exception. Do you mean that the getHibernateTemplate()returns null? But in the same class I have another method called save(...)which looks like this.getHibernateTemplate().save(accident); and it works(I mean no exception is raised. )
This makes me confused.
YanJun Tong
Greenhorn

Joined: Jun 11, 2003
Posts: 27
I tried the getHibernateTemplate() and found that it is null.My test is as follows:
if(getHibernateTemplate()!=null)
{
system.out.println("not null");
}
Unfortunately I got nothing, which means getHibernateTemplate()==null.
Could anybody tell under which condition the getHibernateTemplate()method will return null? thanks
[ September 30, 2004: Message edited by: YanJun Tong ]
YanJun Tong
Greenhorn

Joined: Jun 11, 2003
Posts: 27
could anybody help me?
sanu jony
Greenhorn

Joined: Oct 11, 2004
Posts: 8
hi YanJun Tong
I also got the same error once.
i got that error because i tried to call the DAO directly by creating the object.
(ie:i tried to call a method named item.save from DAO.i think yours is the same problem)
.This is not the currect method.
for rectifying this
you have to create a bean for u r DAO in servlet.xml that should refence the "session factory".
create a property in u r controller bean and reference the DAO bean.
remember that a getter and setter method that supports the DAO should be there in u r controller class.(else it will show exception)
then in the controller class u can call u r DAO methods.

message me after trying this.
if u r not getting again i can send u some shorted codes for the above
YanJun Tong
Greenhorn

Joined: Jun 11, 2003
Posts: 27
Thanks Sanu I found my problem. You are right that I do need to create a bean for my DAO in servlet.xml Thanks for that
Lahori Munda
Greenhorn

Joined: Nov 08, 2004
Posts: 1
Hi All,

I have a problem in same set up i.e. Spring-Hibernate for MySQL. I add my record from a web page and keep the dateAdded field value null. Because in my database dateAdded field map to timestamp. So when a null value is passed MySQL will give it a current time stamp. I can successfully add a record in the database by using

But when I retrieve the object in the same method call i.e. after saving the record in the database I want to retrieve it back to display. The value of dateAdded remains null which causes NullPointerException during date formatting.

To me it looks like a committ problem, that it has not done a committ, and it is retreiving the object in a same connection.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Lahori Munda:
But when I retrieve the object in the same method call i.e. after saving the record in the database I want to retrieve it back to display. The value of dateAdded remains null which causes NullPointerException during date formatting.
When you save an object, it doesn't read it back from the database. As well, it's now in the Session (unless you clear() it or open a new Session) and will be returned when read in the future. Finally, if you are using a second-level inter-Session cache, you'll read the old object without the timestamp from there instead of the database.

If you need to refresh the object with the database, which you need to do to pick up the database-generated timestamp field, you can use (IIRC, check the JavaDocs)I'm new to Hibernate myself, so I don't know off the top of my head whether or not that bypasses the second level cache. I recall reading that it does, however.

Another option (I think refresh() does this) is to acquire a read lock on the object using lock(object, lockType).
Paul Jr. Durcek
Greenhorn

Joined: Dec 01, 2004
Posts: 1
I am new to Spring and Hibernate and am having a similar problem. I keep getting a java.lang.NullPointerException when I try to access this: from within my DAO object which is called from the Spring service implementation by: and that is called by the form processing method by: Am I missing something obvious? Please help.

- Paul

[ December 01, 2004: Message edited by: Paul Jr. Durcek ]
[ December 01, 2004: Message edited by: Paul Jr. Durcek ]
Davidh henry
Greenhorn

Joined: Mar 04, 2012
Posts: 1
thanks sanu.... you saved me a lot of debugging...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sping+hibernate got NullException
 
Similar Threads
Struts/Spring framework inside JBoss portal
confusion?
Getting a ClassCastException upon executing getHibernateTemplate().find(
Need sample source skeleton
Using Hibernate to insert into Oracle Blob, then read back out