详细说明:本书是使用Eclipse逐步介绍Hibernate,Spring,HSQL和Maven的指南,其中包含4个有关流行框架的教程。Notations
Command prompt:
c\ Command Prompt
口X
C:\tutorials\simpleWeb>mun clean package
Eclipse
Java EE- Eclipse Platform
Project Run window Help
已 Servers
图 Probl Task I回 Prap 4i4 Serv销 Data E Snip曰cons23
器 simpleWeb
No consoles to display at this time
Outline x
A o line ie rok svsilsble
File Explorer or Windows Explorer
画 AruL Project
回回区
File Edit View Favorites Tools Help
Folders
ddress C: \Arul-Projects
Folders
X Name▲
Size Type
日 tutorials
A0371 Pay At Pump Rollout…
File Folder
+Simple
L0371-Pay At Pump Rollout
File Folder
t lo simpleweb
Ojava-success-home
File Folder
4 USERDATA
amaven-book
File Folde
omaven-ecipse-java
File Folde
品 DVD Drve(D:)
42 objects(Disk free space: 25.3 GB)
417NM
Internet Explorer:
Cannot find server-Microsoft Internet Explorer
File Edit View Favorites Tools Help
⊙·国图哈pmm的,回,回冯
Addressahttp:/locahost:080/simpleweb
v+GoLinks
Tell me your name:
Hello
:2 Internet
Tutorial 4-Hibernate, HSQL Database, Maven and Eclipse
This tutorial assumes that you are familiar with Java, Eclipse and maven. If
notpleasereferTutorials1-3athttp://www.lulu.com/content/1080910.This
tutorial is a continuation of tutorial 1(Java, Eclipse and Maven)
Hibernate is an ORM(object to Relational Mapping) tool, so we need a relational database
To keep things simple, I will be using HypersonicSQL (aka HSQL) database, which is easy
touseThisisanopensourcedatabasewhichcanbefoundathttp://hsgldb.sourceforge.net
Alsocheckhttp://hsqldb.org
a HSQLDB Micr
rosDft Internet Explorer
巴回囟
File Edit View Favorites Tools Help
Bk:x的s-hFss
HSOL Hsdb <100%/ Java Database
creatures> <[AQ> script file. The script file is
read the next time the database is opened and the memory tables are recreated with all
their contents. So memory tables are persistent It is important to remember that the data in
memory is written to the script file when you shutdown your database
properly/naturally by executing SQL SHUTDOWN (COMPACT I IMMEDIATELY The
saved subname script> file will load the data into memory the next time the HsQLDB server
starts up. But if you stop the HSQLDB server abruptly in the command line by pressing [Ctrl
[ci the data will not be written to the script file and consequently lost Refer documentation
for cached text tables
InstallHsqldatabaseintoc:javafolderfromhttp:/hsgldb.sourceforge.net
Download the hsqldb_1__7.zip and unpack it into your c: /java folder
A lib
回区
Fle Edit View Favorites Toos Hel
Bk,· bsearch Folders画国,
s白a小地
Size Type
Date Modified
629 KB Executable Jar File
t口 apache-ant-165
当 servlet. jar
4 B Executable Jar File 6/10/2002: 23 PM
t eclipse
It-eeclpse-tutorial-workspace
国doc
f testrun
dk150
3 objects(Disk Free space: 252GB)
Start the HSQL database server by executing the following command in a command
ompt as shown beloy
C: javathsqldbjava-cp /ibhsqldb jar org. hsqldb Server
c: Command Prompt- java-cp /ib/hsqldb, jar org. hs qldb Server
/lib/hsdb
rg. hsqldb S
readMan
atse) entered
[Serverl
nread [main
bRunn
lse) exited
[Server
Startup sequence initiated from main(> method
[Servere1a758
oaded properties from IC:\java\hsqldb\server properties
IServerela758cb]: Initiating startup sequence
[Servere
8cb: Server socket opened successfully in 5
[Serverela758cbi: Database
le:test, alias=] opened sucessfully in 406ns
46m
[Servere1a758
2007-98-8718:59:49.193 HSQLDB server1.8.ison1in
rela758cb]: To close normally, connect
xecute SHUTDOWN SQL
[Serverela758cb]: From command line, use ICtrl]+[C] to abort abrupt ly
Since I have not given any database name and or alias(refer HSQLDB document and/or type
C: java\hsqldb>java-cp /ib/hsqldb jar org. hsqldb Server -? For more details), it defaults to
test as the database name alias After starting the HSHQL database server the following
files are created under "C: javahsqldb'>testIck, test log, test properties
Open up another command prompt and start the Database Manager, with which you
can execute SQLS. If you are new to sQL, then this is handy to practice and gain
some SQL skills. Note: You need to have the hsQldb server running before you
can open the databaseManager.
C: java\hsqldb>java-cp/ibhsqldb jar org. hsqldb util. DatabaseManager
cA Command Prompt-java-cp /ib/hsqldb jar org. hsqldb util. DatabaseManager
Nava\hsqldhiJava -cp -/11b/hsqldbjar org. hsqldbutil. DatabaseManager
This will spawn a new window as shown
connect
Recent
Recent settings
Clr
Setting Name
Type
HSQL Database Engine Server
Driver:
org. hsqldb jdbc Driver
URL
jdbc hsqldb hsq localhost
User
sa
Password
Ok
Cancel
Select HSQL Database Engine Server"and click ok". Now you should have the following
window opened, where you can type in your SQL and execute it by clicking on the"Execute
button. The results will be shown at the bottom You can clear the sQl with the clear
button
HSQL Database Manager
回区
AIe- view Command Recent Options Tools
E jdbc hsgldb hsql: /localhost
type your SHEre
由 Properties
Clear
Execute
Let's try executing the following SQL, which creates a table named"Course"and
inserts some values and then select those values
create table Course(course id integer, name varchar, course varchar, PRIMARY KEY(course id));
ert into
lues(1, Sam, Java)
insert into Course values(2, ' peter, J2EE );
insert into Course values(3, paul, JSF
ert into Course values (4, jonathan, 'Hibernate");
insert into Course values (5, james, Spring)
select from Cours
Copy and paste the above SQL into where it says--type your sQL here -and press the Execute
button to see the results as shown below
HSOL Database品an
Flle view )Command Recent Options Tools
ert into Course values (4,jonathan, Hibemat
日 idbc: hsqldb. hsql. ocalhost
日 COURSE
pe: INTEGER
john Java
Type: VARCHAR
出rdi
Readonly fa se
Driver HSQL Database en
Product: Hsol D:
Note: After executing select View >"Refresh Tree" from the menu to see the"COURSE on the left
window. Also note the files generated under c: javahsqldb.
Note: The syntax for these files are properties, s databasename> Ick etc. Since we
have not specified any database name, it defaults to "test". Refer HSQL documentation for starting the
server with database name. e.g. java-cp /ib/hsqldb jar org. hsqldb Server-database. 0 file mydb
dbname. 0 xdb, where xdb is the alias
Note: To persist your data from memory to"test script, you need to execute the SHUTDOWN SQL
command as shown below
HSQL Database Manager
回
File View Command Recent Options Tools.
E jdbc: hsqldb hsql: ocalhost
(SHUTDOWN COMPACT
田 Properties
Execute
SHUTDOWN [COMPACTIIMMEDIATELY]
(HSQLDB SQL only
cck→
Now you should have the"test script"file created under"C: javakhsqldb
Fie Edit View Favorites Toos Help
Search
Folders图-
ypa
口jv
Fie Folder
1705/20055:47FM
±(,m2
□bud
Fie Folder
12(07/20051:08M
it o apache-ant-1.6.5
Fie Folder
11/0/20067:00PM
t o eclipse
12/07/20051:05A
0 echpse-tutorial-workspace
Fie Folde
160/2001:58PM
+a-facelets 11 13
doest
Fie Folder
406/20011:10PM
java-cp / ib/hsqldb jar org. hsqldb Server
Also open up the Database Manager
C: java hsqldb>java-cp /ib/hsqldb jar org. hsqldb. util. DatabaseManager
Firstly, we will set up the maven related pom. xml file configurations
Open your eclipse with the workspace C: javaleclipse-tutorial-workspace
Create a"resources" folder under"simple/src/main
Run the following maven command from a new command prompt
C: tutorials simple>mvn eclipse: clean eclipse: eclipse
Refresh the simple" project in eclipse. Now create a java package named com. mytutorial
under simple/src/main/resources
Your eclipse workbench should look something like
区
Fle Edit Navigate Search Project Run Window Help
r,固画:
·國③isis:目
C Project Explorer
日年|
rvers
For resource files like
理 com. mytutoria
roperties,*.xml etc
t-C srcjtest/java
+-E RE System Library [ire1 5.0]
com,myt.sources
日图四口鼎岛崩距
Open the "pom.xml file under"simple" to add dependencies like Hibernate and HSQLDB java
driver. Add the following in bold. To learn how to identify dependency library coordinates refer
Tutorial3onjsf,MavenandEclipse"fromhttp://www.lulucom/content/1080910
project xmins="htTp: /maven. apache org/pom/4.0.0
xminsxsi="http://www.w3.org/2001/xmlschema-instance
Xsischemalocation="http:/maven.apacheorg/pom/4.0.0
http://maven.apacheorg/maven-v400.xsd">
4.0.0com. mytutoriaksimple jars/packaging>
1.0-SNAPSHO Tsimple
Hibernate library and its
grouped>units/groupld>
transitive dependencies. Ic
sartifactld>junit
Maven will look at ibcrnatc's
3.8.1
pom file and bring in all its
testorg. hibernatehibernate
3.2.4.gas/version>
hsqldb
HSQL database JDBC driver
hsqldb1.8.0.7
In maven everything is done
through plug-ins. This is the
maven java compiler plugin. I am
using Java 5(i.e. JDK 1.5)
lugin>
org. apache maven. plugins maven-compiler-plugin 2.0.2
configurati
c/plugins>
By default Maven2 looks into your local repository
c: java. m2\repository and its default remote
epositoryhttp://repol.mavenorg/maven2/.or
of hibernate's dependency jars "jta-101B. jar"is
only available at Java Dev repository at
http:/iDownload.javanet/maven/2,sowedefine
maven-repository dev java. netJava Dev Net Repository http://download.javanet/maven/2/
trues/enabled>
neverfalse
≤
Note: You can add any number of repositories and maven 2 will look through them one by one
includinglocalrepositoryc:java\.m2repository&http://repol.maven.org/maven2/)untilthe
dependent jar file is found
ahttpfrepn1mavennrgimaven2'nrg/hi
⊙水·日·国哈Fe的,居回遇
l. URG2
oFtional>true c/ optional
cori nTd>jhnssjboss-cache1.2.2=/verse
opTional true/ort
etmauen
grouoId>lava .transaction groupld
1
Bmvn eclipse clean eclipse: eclipse
e Properties for simple
ype fiter text
Java Build path
eaninfo fath
I C Source B Projects ad Lbranes% Order and Export
Deva BulPath
e M2 REPojanthr/antlr 2.7. 6/antlr-27 6. jar-C laval, m2repositaryantlr'antr2, 7.6lantr-27.6.i
t Java Compiler
9 M2_REPO/asmyasm/1.5.3/asm-1 5.3. jar-Cijaval, m2yrepositDry\\ 1. 5.3\asm-1, 5.3, jar
e M2_REPOfasmfasim-attrs/1. 5.3 asmrattrs-153 jer-C:\ m2yrepositoryasmyasm-attrsi1,5.3asm-attrs-1 5.3 jar
Ad External J4R
+ Java Edtor
t-e M2_REPo cglib/ cgib)2. 1__3cclib-2 1_3, ja-C: lavay, m2yrepositcrycgib\cglib\2. 1_3hcglib-2 1_3. ar
Add wariable
Profilb-Compliancc and vaidstic# M2 REPO)commons-collectiors commons-colections/2. 1.1/fcommons-cllections-21 1 jar-C: aval. m2repositoryi: onmons-ccllectionsico
jet References
e●HeREpoomnegrer0orn:4cy.n2vestornrdogrgomoegAdubry
Refactoring Hstory
t-e M2_REPO dom+i/dom4j/ 1. 6. 1/ dcm4J-1 6 1. jar-Cljava m2yrepositorr 'id]\]. 1. 6. 1dom4j-1 6. 1. jar
Run/Debug Settings
FPhq51807h107iame0ryhhhd18.7hs:180,J
Server
te M2 REPo/ievax/t arsactianta/1. 0. 1B/ta-1.0.1B, iar -C: liava'\ marepositaryljavaxitransactionlital1 0 1Blita- 1, 0. 1B. jar
Task, Repository
E量t
t e M2_REPOnet/sf/ehuache/ehtache/1. 2. 3 ae-1. 2. 3. jar -C: java
m2yrepusitur y ret\ ye icecha\ 1. 2 3\ehcachie-1 2 3. jar
validation
t dN2 REPO/org/hbernatejhberna e/3. 2. 4. galhibernabe-32. 4. ga jar -\. m2yrepostory\or g hibenabelhibernatel 3 2. 4. gathbernate
+= JRE System Library Lrel..0J
⑦
You can open the properties window on"simple" project by right clicking and selecting
properties". You can see the jar files for hibernate and its dependency jars like asm, cgilib
ehcache etc based on hibernate's pom file where dependencies are defined Open
hibernate s pom file shown below and check the dependencies. Also you can find the hsqldb
java driⅳer.
a Index of /maren 2forg/hibernate/hibernate/3. 2.4.ga- Microsoft Internet Explorer
回区
Favori es Tools Help
图合pwmm,回·回器
习6umks”
Index of mavcn2/org/hibernate/hibernate/3.2. 4.ga
a己
Parent directory
⊥2
h1bernate-3,4.1. ca-lavacoc ]ar: ma5 Iz-May-ZI
h1oernave-3,2.t. ca-lavadoc, ar: sha_ 12-1
hibernate-3.2..-suce.1ar·shs⊥12-M
eae-3·2.号.a,P
_03.3R
h1Dernate-3.2.4.ca pom Ic5
L33