?
Solved

spring Accessing Hibernate through HibernateTemplate

Posted on 2011-05-06
9
Medium Priority
?
664 Views
Last Modified: 2012-05-11

I was reading folowing lines. And I did not understand clearly.
Accessing Hibernate through HibernateTemplate
The template-callback mechanism in Hibernate is pretty simple. There is the
HibernateTemplate and one callback interface: HibernateCallback. And the
HibernateCallback interface has just one method:
Object doInHibernate(Session session)
throws HibernateException, SQLException;
As you can see, the HibernateCallback interface is pretty straightforward. Now,
let’s put the HibernateTemplate to use. We’ll begin by getting an object from
the database:
public Student getStudent(final Integer id) {
return (Student) hibernateTemplate.execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
return session.load(Student.class, id);
}
});
}
Since we are using an inner class, a little more code is required and is not quite as
clean as when we were not using Spring’s Hibernate support. But we can have it
both ways—clean code and Spring Hibernate support. The HibernateTemplate
class provides some convenience methods that implicitly create a HibernateCallback
instance for you. All you have to do is call one of the convenience methods
and Spring’s framework does the rest. For example, here is how you would take
advantage of one of these methods to accomplish the exact same thing as we did
earlier—get an object from the database:

Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
0
Comment
Question by:gudii9
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35712007
This page gives examples of using both of HibernateTemplate in both ways mentioned above:
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/classic-spring.html
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35712982
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 664 total points
ID: 35712984
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Author Comment

by:gudii9
ID: 35725081
my question is why are we using inner class here. please advise
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35725118
In section A.1.1.1 of the  chcapter that I mentioned above
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/classic-spring.html
they show how HibernateTemplate can be used in both ways using convenience methods or HibernateCallback interface,
the latter is using anonymous classes in the similar manner as we use anonyomus classes for registering events
in GUI applications. Try to read it. Though, it is certainly not something very easy to unserstand, I'd agree.
0
 
LVL 7

Author Comment

by:gudii9
ID: 35725150
>>both ways using convenience methods or HibernateCallback interface,

what is difference between above two methods. please advise
0
 
LVL 47

Accepted Solution

by:
for_yan earned 668 total points
ID: 35725484
HibernateTemplate provides some methods, like find in the example of the link above
which allow to get data without using Hibernate API.
That's what is shown in the first box there.
These methods take care of opening and closing session within themselves.
However not all methods of Hibenate are exposed in this way by HibernateTemplate.
There are some methods of Hibernate session which are not
exposed thrugh HibernateTemplate.
As an example in the link above  they show methods of Hibernate Criteria which you cannot access
through HibernateTemplate directly.
For such situations HibernateTemplate has method execute(),
which takkes as a parameter instance of the class
 which inherits from HibernateCallback
and overrides doInHibernate(session)  method.
Using this execute metod you can get access
to wider capabailities of Hibernate session.
usually instead of cvreating a spearate class and
feed its instance to execute method, it is common to use anonymous
class in the same way we use anonymous classes for
feeding the instance of event listeners when writing regular Swing applications.

Frankly, this is difficult stuff  to understand just from reading book,
when you start doing something along these lines you'll
get much better feeling.






0
 
LVL 92

Expert Comment

by:objects
ID: 35725867
> my question is why are we using inner class here. please advise

just for convenience, theres no real need to

you could also write:


public Student getStudent(final Integer id) {
   return (Student) hibernateTemplate.execute(new MyCallBack());
}


class MyCallback implements HibernateCallback {
     public Object doInHibernate(Session session)
        throws HibernateException {
            return session.load(Student.class, id);
     }
}

let me know if you gave any other questions

0
 
LVL 92

Assisted Solution

by:objects
objects earned 668 total points
ID: 35725929
> what is difference between above two methods. please advise

Well there aren't actually two methods of using HibernateTemplate, so I'll assume you're talking about using HibernateTemplate versus not using it.

main difference is with handling exceptions. HibernateTemplate does not allow you to throw unchecked exceptions, whereas calling support methods directly you can throw them.

In general though, HibernateTemplate is simpler than calling directly. And results in cleaner code
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question