|
||||
Table of Contents |
||||
1. IntroductionWelcome to the MyEclipse Introduction to Hibernate tutorial. In this tutorial we are going to cover some of the basic features of using the Hibernate framework, such as OR-mapping from within MyEclipse. At it's core, Hibernate is an OR-mapping technology that is used to map database structures to Java objects at runtime. Using a persistence framework like Hibernate allows developers to focus on writing business logic instead of writing an accurate and performant persistence layer (which includes, DAOs, SQL queries, JDBC code, connection management, etc.).
However, to begin using Hibernate, you normally must generate the
proper persistence mappings for Hibernate to manage, set up the
database connection information and code the DAOs that read or
write your mapped entities. Fortunately, MyEclipse developers
don't need to do any of that because MyEclipse will generate all
of this code for you. And that bit of code automation frees you
up for the most important task of all: writing your application
logic.
|
||||
2. Suggested AudienceThis 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. While this tutorial will also introduce some of the basics of Hibernate, it is not intended to replace the detailed Hibernate Reference Guide found on the Hibernate site and in the Resources section below. There is a lot of functionality in Hibernate that allows it to scale to support enterprise-class applications, and those details can be learned from reading through the reference or a good Hibernate book.
If either MyEclipse or Hibernate makes you feel uncomfortable,
this introductory tutorial should provide you with the basics of
both. If you wish to learn more about either MyEclipse or
Hibernate please have a look at either our product
Documentation for more
material or our
Resources section
respectively.
|
||||
3. System Requirements
This tutorial was created with MyEclipse 5.1 and the bundled
Hibernate 3.1 libraries. If you are using a another version of
MyEclipse or Hibernate, 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. Introduction to Hibernate
In the early years of Java database and web programming,
developers accessed their databases using the different classes
provided by the
java.sql package; you may even remember doing this
yourself. Such usage consisted basically of getting a
Driver from the
DriverManager, creating a
Connection, using the
Connection, handling exceptions properly, closing the
connection and so on. A common problem at this stage was
forgetting to clean up database connections and getting
connection exceptions in your applications after they had been
running for a while.
A few years later "connection pools" became a big topic
since they allowed the developer to stop worrying about creating
and managing (cleaning up) DB connections and instead focusing on
their SQL and
ResultSet parsing code. Suddenly, we had mostly solved
the problem of connection exceptions to the database in
long-running applications. However, it was still common to see
hundreds of lines of boiler-plate code used to populate queries
with values and parse the
ResultSets returned from SQL queries.
A few more years went by and someone had an idea to automatically map ResultSet results directly to Java objects, which mostly solved the problem of all the redundant boiler-plate parsing code. At that point in time Java database development had taken a several big steps forward and was getting much simpler. Then Hibernate came into the picture.
With Hibernate came the idea that not only would it continuing
doing all these automated things for you, but it would also
manage the state of your objects in memory and
it would worry about when and how object values would be
"read from" or "written to" the database. Now
all the sudden developers were dealing completely with objects
(or mapped objects) and letting Hibernate handle everything else.
Developers were no longer writing JDBC and SQL code at all.
Instead they were using code that automatically did all that work
for them.
At the time Hibernate came on the scene, the other persistence technology out there was EJB 2.x. Hibernate's timing, ease of use and power all contributed to one of the fastest uptakes of a technology that the Java space that had been seen in quite awhile.
In this tutorial we are going to take a look at how using
MyEclipse with Hibernate can make your life even easier than
using Hibernate alone. In fact, MyEclipse removes the need to
write any of the Hibernate mappings or configuration files by
completely generating the persistence side of your Java
application in just a few seconds.
We will start off with a simple database that we will
reverse-engineer into a Hibernate-enabled project. Then, we will
write some simple Java code to utilize the Hibernate code that
MyEclipse generated automatically in order to store, retrieve and
update information in our database.
|
||||
5. 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 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:
Now that we have a working connection to our database, the second thing we need before we get started is a Hibernate-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 capabilities to that project from the MyEclipse menu, like so:
|
||||
6. 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 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:
Now that our table has been reverse-engineered into our project, there are all sorts of Hibernate tools we can use in MyEclipse to work with those objects (even without writing code!). The first tool we will look at is the HQL Editor. The HQL Editor, and other HQL views in the Hibernate perspective, assist in the development, evaluation or testing of HQL queries. HQL is a SQL-like language called "Hibernate Query Language". It can sometimes look like simplified SQL and uses object names and references instead of table and column names. A great place to learn about HQL is the Hibernate Reference Document in our Resources section.
With the HQL Editor you can actually write HQL on the fly into
the editor, then run it. The editor, utilizing the objects that
MyEclipse has reverse-engineered from the database, will actually
translate the query to SQL (shown in the bottom right) and then
run it. The result is returned in Java objects and is shown
in the bottom left corner. Let's have a look at how this works
now:
|
||||
7. Writing and Running Hibernate CodeIt is important to be familiar with the tools that MyEclipse provides, but since we've just taken a look at some of the nicer tools included, it's time to start writing our own code! As mentioned before, one of the nicest parts of using MyEclipse to work with Hibernate is the fact that it generates all the boiler-plate Hibernate mapping and even DAO code for you. This means that after you are done reverse-engineering a database you are ready to start writing your application to read, write and update objects in your database. In this tutorial we will write a series of simple methods that do 3 things in the following order:
The three methods (
addUser, listUser, changeUser) are all called from the
main method in our new class. To better understand how
this Hibernate code is written, let's create that class, put
those methods in the class (using copy & paste) and then
review them line-by-line:
After looking at that code we can see how straight forward everything is. We use the DAO classes that MyEclipse generated for us to get, update and save our objects to the database and MyEclipse/Hibernate take care of all the other details for us. It couldn't get much easier than this. Now to the fun part, let's run the code and see if it actually does the right thing:
Very nice, it worked correctly just as we had hoped! We used the
HQL Editor to query our database and make sure that our user was
saved to it. Additionally, we could have just as easily switched
to the Database Perspective and queried the database from there
to see that the user record was in the table.
|
||||
8. Conclusion
While the application in this tutorial may seem simple, the
techniques and information provided is critical to understanding
both Hibernate and MyEclipse. The fundamentals presented in this
tutorial apply to pretty much any Hibernate-enabled application
you would want to develop: the core idea of object mapping to the
database. After getting these basics working, you are free to
enhance, extend or change your application in any way you need to
and be assured that MyEclipse will continue to help you develop
and extend it.
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. FAQ
|
||||
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
Basics
|
||||
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. |