Exercise -- Getting Started With Hibernate You will verify your Hibernate set up by compiling and running a small Hibernate application. Part One. Lab Setup. Download and install Eclipse, Hibernate and HSQLDB as described in the document "Hibernate Lab Setup."

shopholisticΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 6 μήνες)

87 εμφανίσεις

Exercise
--

Getting Started With Hibernate


You will verify your Hibernate set up by compiling and running a small Hibernate application.


Part One. Lab Setup.


Download and install Eclipse, Hibernate and HSQLDB as described in the document "Hibernate Lab

Setup."


Part Two. Establishing the Database Connection.


Start a command / Terminal window, and change to the directory where your HSQLDB.JAR file is
located. Execute the following command to start the HSQLDB database manager:


java
-
cp hsqldb.jar org.
hsqldb.Server
--
database.0 file:mydb
--
dbname.0 xdb


This will start a database called "mydb." Your window should look something like this:




Next, enter the following command in the upper right
-
hand window, then click on the "Execute SQL"
button:


CREAT
E MEMORY TABLE MESSAGES(MESSAGE_ID BIGINT GENERATED BY DEFAULT AS
IDENTITY(START WITH 1) NOT NULL PRIMARY KEY, MESSAGE_TEXT VARCHAR(255),
NEXT_MESSAGE_ID BIGINT, CONSTRAINT FK_NEXT_MESSAGE FOREIGN
KEY(NEXT_MESSAGE_ID) REFERENCES MESSAGES(MESSAGE_ID))


AL
TER TABLE MESSAGES ALTER COLUMN MESSAGE_ID RESTART WITH 3


You probably won't want to type all of this in, so you can copy and paste this from the text file
SCHEMA.TXT.


Be sure to leave the database running!


Now, start up Eclipse and create a new Java p
roject called HelloWorld. This first project will use
Hibernate to store and retrieve text messages from the database. Within the src folder, create two
packages, called hello and persistence. Copy HelloWorld.java, Message.java and Message.hbm.xml
into

the hello package. Copy HibernateUtil.java into the persistence package. Copy hibernate.cfg.xml
and log4j.properties into the src folder.


Let's take a brief look at each of these files. Message.java just contains the class called Message,
which will b
e mapped to the table called MESSAGES. HelloWorld.java contains the "main" class,
HelloWorld, which uses several transactions to create, store and retrieve the messages. Hibernate's
transactions are handled through a Session, and a SessionFactory object
creates and manages a
Session's connection to the database. The HibernateUtil.java file contains several convenience utilities
encapsulated in the static HibernateUtil class.


Hibernate's "main" configuration file, hibernate.cfg.xml, resides in the "src"

directory of our project,
and is used to configure (among other things) the database connection and database type. If you look at
this file, you will see that the "hibernate.connection.driver_class" property is set to an HSQLDB driver,
the "hibernate.con
nection.url" is set to an HSQLDB connection URL, and the "hibernate.dialect" is set
to the HSQLDB dialect of SQL. If you were going to "port" your Hibernate application to a different
database


for example, Oracle


you would need to change all three of

these properties to the correct
values for Oracle. The "show_sql" property will cause Hibernate to display its generated SQL when
run, and the "format_sql" property will cause it to be nicely formatted. The "mapping" property at the
end of the XML file
tells Hibernate that it needs to look at the Message.xml file in the Hello directory
to understand how to map the Message java object. (You need to have a mapping file, or an annotation,
for every Java class that will be saved to a database.)


Finally, th
e Message.hbm.xml file in the Hello directory tells Hibernate how to map the Message
object. The <class> tag says that the class Message, in the Hello package, maps to the MESSAGES
table. The <property> tag says that the text member maps to the MESSAGE_
TEXT column. The
<many
-
to
-
one> tag says that the nextMessage member maps to the NEXT_MESSAGE_ID column
using a foreign key constraint called FK_NEXT_MESSAGE. The <id> tag specifies the unique
identifier for the message, and says that the "id" member of t
he object will map to the MESSAGE_ID
column of the table. The identifier will be generated using the database's method of choice


sequence
or identity


because of the "native" attribute.


Hibernate provides lots of information on its behavior through it
s logging capabilities. Those are
configured through the log4j.properties file. The commons
-
collections and log4j JAR files are used by
Hibernate to provide normal and diagnostic output on its behavior. The main things you need to know
about the log4j.p
roperties file is that it goes in the src directory at the same level as hibernate.cfg.xml
and that you can modify Hibernate's logging options by changing the value of
log4j.logger.org.hibernate


the usual value of INFO shows startup and error messages, D
EBUG shows
incredibly verbose information.


Building the Project


Before you build this project, you need to add the Hibernate JAR files to your project build path.
Select Properties from the Project menu, and click on the Libraries tab:



Next, click on

the "Add External JARs …" button, and select all the JAR files shown in the picture.
(It's okay not to have hibernate
-
tools.jar.)


Running Your Application


Save all your files, and then right
-
click on HelloWorld.java, and choose Run As > Java Applicati
on. (If
you don't have that available, simply choose Run As > Run Configurations … and click on Java
Application on the left hand side of the dialog box that appears to create a run configuration.)


The Eclipse toolbar has a green button with a white, r
ight
-
pointing arrow on it on the left hand side of
the tool bar. Simply clicking on that button is equivalent to choosing "Run," and carefully clicking on
the small black drop
-
down arrow to its right allows you to choose either "Run" or "Run
Configuration
s."


If all goes well when running the application, you will see lots of text in red


that's simply Hibernate's
informational logging, so don't be alarmed


followed by this actual work, in black:



Hibernate:


insert


into


MESSAGES



(MESSAGE_ID, MESSAGE_TEXT, NEXT_MESSAGE_ID)


values


(null, ?, ?)

Hibernate:


call identity()

Hibernate:


select


message0_.MESSAGE_ID as MESSAGE1_0_,


message0_.MESSAGE_TEXT as MESSAGE2_0_,


message0_.NEXT_MESSAGE
_ID as NEXT3_0_


from


MESSAGES message0_


order by


message0_.MESSAGE_TEXT asc

1 message(s) found:

Hello World

Hibernate:


select


message0_.MESSAGE_ID as MESSAGE1_0_0_,


message0_.MESSAGE_TEXT as MESSAGE2_0_0_,



message0_.NEXT_MESSAGE_ID as NEXT3_0_0_


from


MESSAGES message0_


where


message0_.MESSAGE_ID=?

Hibernate:


insert


into


MESSAGES


(MESSAGE_ID, MESSAGE_TEXT, NEXT_MESSAGE_ID)


values


(null, ?,
?)

Hibernate:


call identity()

Hibernate:


update


MESSAGES


set


MESSAGE_TEXT=?,


NEXT_MESSAGE_ID=?


where


MESSAGE_ID=?

Hibernate:


select


message0_.MESSAGE_ID as MESSAGE1_0_,


message0_.MESS
AGE_TEXT as MESSAGE2_0_,


message0_.NEXT_MESSAGE_ID as NEXT3_0_


from


MESSAGES message0_


order by


message0_.MESSAGE_TEXT asc

2 message(s) found:

Greetings Earthling

Take me to your leader (please)



The first transaction was

the first INSERT; the second transaction was the
SELECT that caused "Hello, World." to be displayed; the third transaction
was the second INSERT; the fourth transaction was the SELECT that caused
both messages to be displayed.


If you had any problems wit
h "missing" JAR files when you launched your
application, select Run > Run Configurations, click on your "HelloWorld"
Java Application configuration, click on the Classpath tab in the Run
Configuration window and make sure that all of your JAR files are on

the
run
-
time classpath. Add them there, if needed.


Summary


You have successfully downloaded, installed, and configured the Hibernate
open
-
source persistence system.

You have verified your Hibernate configuration by using it to store and
retrieve inform
ation from a database.