Meaningless Drivel is fun!
The moose likes Object Relational Mapping and the fly likes Problem in running Hibernate Sample application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem in running Hibernate Sample application" Watch "Problem in running Hibernate Sample application" New topic

Problem in running Hibernate Sample application

Aman Singh
Ranch Hand

Joined: Oct 30, 2008
Posts: 57
Hi Experts,
I am trying to run a sample Hibernate application from After many tries I couldn't get it to run. Everytime I try to run it, I am getting a "Exception in thread "main" java.lang.NullPointerException".

Can someone help me out with this

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Just a word of warning, the roseindia tutorials are not very good.

As with any NullPointerException there will be a line number telling you where it occured. What happens on that line?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Aman Singh
Ranch Hand

Joined: Oct 30, 2008
Posts: 57
Hi Paul,
Thanks for your quick reply. You are very right, Roseindia tutorials are always full of bugs. For beginners, there aren't any details hibernate sample applications.

I am getting an error on line 34, which is session.flush();

Is it because we aren't able to build the session in the first place?

Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Yeah, I'm not sure if you need to be flushing and closing that session on your first trial example. I'd also say that openSession, as opposed to using getSession, might lead to more problems than it would solve.

I've put together some little tutorials from my book on Hibernate and made them available on my website. They've been tested pretty thoroughly, and if the roseindia tutorials aren't doing it, might I be so humble as to suggest you might take a look:
Getting Started with Hibernate

Here's an example that I often use in setting up and vailidating a Hibernate configuration:

Okay, that's alot of code to throw out here without an explaination, but really, if you want to see what Hibernate is doing, and where the work really happens, look here:

Notice how the session is obtained, a transaction is started, a User is created, the session saves the User, and then the transaction is committed? That's really the key to Hibernate working.

Also notice that in this little example, there's no need to flush or close the session. Sure, once you start deploying a banking application, get those method calls in there in the right spots, but really, why confuse the issue when you just want something to work? Get the basics down, and the more advanced stuff will come very naturally.

Best of luck!

-Cameron McKenzie
Aman Singh
Ranch Hand

Joined: Oct 30, 2008
Posts: 57
Hi Cameron,

I must say , One of the very good explanation :-) for a java newbie like me.

I made it to work, dont know how :-)

I did look at your tutorial while googling it, It is very informative. I didnt try it because I dont have the slightest idea of annotations or what exactly they are.

I agree. Here's the link:
subject: Problem in running Hibernate Sample application
It's not a secret anymore!