|
||||||||||
Table of Contents |
||||||||||
1. IntroductionWelcome to the MyEclipse Hibernate and Spring tutorial. This tutorial is meant to be a quick overview of how using both Hibernate and Spring in the same project works in MyEclipse. Before starting this tutorial, we encourage you to start with first the individual Introduction to Hibernate and Introduction to Spring tutorials in the Resources section that covers the individual technologies in more detail. The project used in this tutorial will be similar to the one used in the Introduction to Hibernate.
The use of Spring with Hibernate will be shown in this tutorial
by creating a layer of abstraction between our persistence code
(Hibernate) and our business logic. The business logic in most
cases will be the "stuff the web application does", for
simplicity sake, in this example it is our
main method. By adding this level of abstraction, we can
use Spring to control the real persistence engine that is plugged
in behind the scenes and used by our business code (main method).
For small applications this may seem inconsequential, but when you start seeing how easy it is to swap out entire portions of your code base using Spring by simply adjusting references in your bean configuration file, it's easy to see how powerful this tool can become. For the most obvious case, it makes testing much easier. You are able to say swap in a persistence implementation that makes all it's calls to an embedded DBMS or maybe logs all it's activity. Then when the application is deployed to production, the persistence implementation is switched back to the code that makes use of the main DBMS and doesn't log anything for performance reasons. There are a myriad of possibilities when using Spring. |
||||||||||
2. Suggested Audience
This tutorial is intended for developers who are somewhat
familiar with either MyEclipse or Eclipse so you are expected to
recognize navigation within the IDE and understand some of the
more common concepts like "Views". Additionally,
developers should be familiar with persistence in Java (JDBC,
EJB, iBatis, JPA, etc.) to be able to understand the role
Hibernate plays more quickly and be familiar with Spring and
dependency injection.
If the reader is unclear on how these individual technologies
work, a much better place to start will be the individual
Hibernate and
Spring tutorials found in the
Resources section
below.
|
||||||||||
3. System Requirements
This tutorial was created with MyEclipse 5.1, the bundled
Hibernate 3.1 and Spring 1.2 libraries. If you are using a
another version of MyEclipse, Hibernate or Spring, most of these
screens and instructions should still be very similar.
If you are using a newer version of MyEclipse and notice portions
of this tutorial looking different than the screens you are
seeing, please
let us know and we will make sure to
resolve any inconsistencies.
|
||||||||||
4. Getting StartedThe project we create during this tutorial, as well as the create-table SQL script for the database table we used, can be found in our Resources section below for those of you that want to peak ahead. For the rest, we would highly encourage you to follow along with the tutorial, creating the project as we go. To get started with Hibernate and Spring in MyEclipse the first thing we need is a connection to the database that we want to build our application to use. In this particular case, it is an instance of MySQL 5 with a sample user table we've already created. We are also using the MySQL Connector/J JDBC driver to connect to our install of MySQL. So let's get started by creating a new connection, in MyEclipse, to our database (Note: This is the same project we created in the Hibernate tutorial as mentioned above):
Now that we have a working connection to our database, the second thing we need before we get started is a Hibernate and Spring-enabled project (Java, Web, Web Service, etc). We can create such a project by creating any of the supported types of base projects, like a Java or Web project, then adding Hibernate and Spring capabilities to that project from the MyEclipse menu, like so:
|
||||||||||
5. Reverse Engineering
Now that we have a database connection and a project properly
configured, the next thing for us to do is tell MyEclipse to
reverse-engineer our database table into Hibernate (Java) objects
and put them into our project.
In addition to that, because our project is a Hibernate-Spring
project, MyEclipse will give you the option to generate
"Spring DAOs" instead of just plain DAOs during the
reverse engineering. This will automatically create Spring beans
for the DAOs that have their
sessionFactory properties correctly configured to the
existing Spring reference to the Hibernate SessionFactory.
In the example below we use the simplest form of reverse-engineering, letting the wizard take all the default values. However, for maximum control you could optionally use the Next button and step through the wizard to select details like primary key generation strategy, object names, types and more. Let's reverse-engineer our table now:
|
||||||||||
6. Writing and Running Hibernate-Spring CodeNow that we have our project setup we are ready to start writing some code. There are going to be two pieces of code we write:
Let's first start with the persistence layer since that is what sits between all the code we just generated and the code we will write in the business logic layer. The code for our PersistenceLayer class will look like this:
The code is fairly straight forward, but let's recap. The
purpose of this class is to get a
UserDAO instance injected into it by Spring and then it
uses that reference under the covers to actually implement the
save/find/update and delete operations without the calling code's
knowledge. This allows us to easily and quickly modify the
persistence code in the application without changing the
application code at all. More specifically, if we ever wanted to
change how PersistenceLayer was implemented, say to manage
exceptions better, transactions or anything else, we could simply
edit this class and be done, there would be no need to refactor
the entire application as long as all the method signatures
stayed the same.
The other important thing to note is that since this layer of abstraction is loosely coupled with the persistence code from the application code, it's easy for us to use Spring to inject say a testing UserDAO implementation that simulates DB operations but doesn't actually perform them. There are all sorts of advantages to this design. Now that we have PersistenceLayer implemented, let's take a look at the business logic (or our main method implementation):
Let's take a quick look at what this code is doing:
Some of you might be asking yourself: " How does the PersistenceLayer get a reference to UserDAO in order to actually execute those persistence operations? I don't see that code anywhere." and the answer to that is: We need to create a new Spring bean (PersistenceLayer) that will get the correct UserDAO instance injected into it at runtime so the code will run correctly. Doing that looks something like this:
Now our application should be ready to run, let's recap the major portions of what we have done:
Now we can actually run our BusinessLogic class and see our application in action:
Very nice, it worked correctly just as we had hoped! We can see
we were able to store, update and delete our user from the
database with just a few lines of code. Imagine how easy writing
the rest of this application will be since all we need to do is
use our
PersistenceLayer.
|
||||||||||
7. Working with Hibernate Annotations and SpringMyEclipse 6.5 introduces Hibernate Annotations support for both standalone Hibernate projects and for Spring Hibernate projects. The following section presents a brief overview of our support for Hibernate Annotations and Spring. Creating a Hibernate Spring project with Annotations SupportFollow the steps in Figure 2 above to create a Hibernate and Spring enabled project. The following changes are necessary to enable annotation support.
Reverse Engineering
|
||||||||||
8. ConclusionAs was shown in the Introduction to Hibernate and Introduction to Spring tutorials (see Resources) working with these technologies in MyEclipse is very straight forward and easy. What we tried to show in this tutorial was not only why using these technologies together is a good idea, but how MyEclipse will even help you develop using the technologies in the same project.
We hope you have found this tutorial helpful. If you had comments
about this tutorial or suggestions/questions for us, please
let us know. We always value our user's
feedback especially on educational materials such as these.
|
||||||||||
9. FAQNone at this time |
||||||||||
10. ResourcesBelow are links to resources that we hope will help answer most of the questions you could have while working your way through this tutorial pertaining to Hibernate: Files
Reference |
||||||||||
11. FeedbackWe would like to hear from you! If you liked this tutorial, has some suggestions or even some corrections for us please let us know. We track all user feedback about our learning material in our Documentation Forum. Please be sure to let us know which piece of MyEclipse material you are commenting on so we can quickly pinpoint any issues that arise. |