This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Spring and the fly likes Problem using hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Problem using hibernate" Watch "Problem using hibernate" New topic
Author

Problem using hibernate

Nitin Menon
Ranch Hand

Joined: Jun 13, 2007
Posts: 79
Hello friends,

I am having this problem while using hibernateTemplate to save the contents of an annotated POJO class into database using Hibernate. My application uses Spring to handle the services and Hibernate (org.springframework.orm.hibernate3.HibernateTemplate) to handle the database functionality. In my Data Access Object (DAO) class, I have a simple method which gets a VO (POJO) class and saves it to the database using hibernateTemplate.save(obj). The class is :



When the above method is run, a "null" is being printed on the console log and it is not being saved into database. There is no other error shown. There is no null values in the VO class as I've tried printing the values in it. In the spring applicationContext, I've created a bean for this class as :



The annotated VO class looks like :


I am running this application on apache tomcat 6 and MySQL database. Can you please help me identify problems in the code if any. Do you want me to put the log appearing in the console on application startup?
Thanks in advance.
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

Since you created new topic for this, lets continue the discussion here.. First, set show_sql to true, and tell us, whether you see an insert query in the console. It prints null in the console, means, when it prints null? when you check for hibernateTemplate.contains() or where?
Generally, if data is not saved in backend means, data is not flushed to the backend, what do get when you print hibernateTemplate.getFlushMode()?


Regards, Prasad
SCJP 5 (93%)
Nitin Menon
Ranch Hand

Joined: Jun 13, 2007
Posts: 79
Prasad Krishnegowda wrote:Since you created new topic for this, lets continue the discussion here.. First, set show_sql to true, and tell us, whether you see an insert query in the console. It prints null in the console, means, when it prints null? when you check for hibernateTemplate.contains() or where?
Generally, if data is not saved in backend means, data is not flushed to the backend, what do get when you print hibernateTemplate.getFlushMode()?


it prints null on the console when it encounters the first hibernateTemplate method. I'll try both hibernateTemplate.getFlushMode() and also will set show_sql to true.
Nitin Menon
Ranch Hand

Joined: Jun 13, 2007
Posts: 79
Nitin Menon wrote:
Prasad Krishnegowda wrote:Since you created new topic for this, lets continue the discussion here.. First, set show_sql to true, and tell us, whether you see an insert query in the console. It prints null in the console, means, when it prints null? when you check for hibernateTemplate.contains() or where?
Generally, if data is not saved in backend means, data is not flushed to the backend, what do get when you print hibernateTemplate.getFlushMode()?


it prints null on the console when it encounters the first hibernateTemplate method. I'll try both hibernateTemplate.getFlushMode() and also will set show_sql to true.


I tried both of them. I dont know why nothing is getting printed there other than a "null".
The logs at the startup of the application are:


I hope there is nothing fishy in there.
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

First make sure that, hibernateTemplate is not null.. If its null, use the ClassPathXmlApplicationContext of spring and get the bean.. and try ivoking the method using this bean..
Nitin Menon
Ranch Hand

Joined: Jun 13, 2007
Posts: 79
Prasad Krishnegowda wrote:First make sure that, hibernateTemplate is not null.. If its null, use the ClassPathXmlApplicationContext of spring and get the bean.. and try ivoking the method using this bean..


Can you make that little more clear? What exactly should I do? I am not too good at the configuration part.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Where are you defining which methods are transactional. My first guess is that it is not transactional, and Hibernate requires a transaction. My guess would then be that there is no Session in the template to run the data access code, and it is throwing a DataAccessException which is an unchecked exception.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

Also, in catch block, instead of System.out.println(e.getMessage()); print the stackTrace and can you paste that stack trace here, so that we can get to know, what is throwing an NullPointerException..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem using hibernate
 
Similar Threads
Spring DAO + Hibernate; is this right?
Hibernate, Spring and H2
data not persisted using hibernate spring integration
Hiberante transaction in Spring
Error running the application