没有合适的资源?快使用搜索试试~ 我知道了~
beginning hibernate
3星 · 超过75%的资源 需积分: 9 12 下载量 187 浏览量
2009-06-16
12:54:47
上传
评论
收藏 394KB PDF 举报
温馨提示
试读
38页
Hibernate an open source Java persistence framework project. Perform powerful object relational mapping and query databases using HQL and SQL.
资源推荐
资源详情
资源评论
Dave Minter and
Jeff Linwood
Beginning
Hibernate
From Novice to
Professional
6935fm_final.qxd 8/2/06 9:44 PM Page i
B
eginning Hibernate: From Novice to Professional
Copyright © 2006 by Dave Minter, Jeff Linwood
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-59059-693-7
ISBN-10 (pbk): 1-59059-693-5
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the
US and other countries.
Apress, Inc. is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement
from Sun Microsystems, Inc.
Lead Editor: Steve Anglin
Technical Reviewer: Sumit Pal
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick,
Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser,
Keir Thomas, Matt Wade
Senior Project Manager: Kylie Johnston
Copy Edit Manager: Nicole LeClerc
Copy Editor: Damon Larson
Assistant Production Director: Kari Brooks-Copony
Senior Production Editor: Laura Cheu
Compositor: Linda Weidemann, Wolf Creek Press
Proofreader: April Eddy
Indexer: Michael Brinkman
Artist: April Milne
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail
orders-ny@springer-sbm.com,
or visit http://www.springeronline.com.
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley,
CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at
http://www.apress.com in the Source Code section.
6935fm_final.qxd 8/2/06 9:44 PM Page ii
Building a Simple Application
In this chapter, you’ll take another look at some of the steps necessary to get the example
from Chapter 1 up and running. You’ll also build a somewhat larger application from scratch.
All of the code in this book is available for download from the Apress site (
www.apress.com).
Installing the Tools
To run the examples in this chapter, you will need to install a number of tools. You will require
a JDK, the Hibernate and Hibernate Tools distributions, the Ant build tool, and the HSQLDB
database. Table 3-1 lists the specific tools you will need and where you can find them.
Table 3-1. The Tools Used in This Book
Tool Version Download Location
Hibernate 3.2.0 http://hibernate.org
Hibernate Tools 3.1 http://hibernate.org
Ant 1.6.5 http://ant.apache.org
HSQLDB 1.8.0.2 http://hsqldb.org
Hibernate and Hibernate Tools
The latest v
ersion
of H
ibernate is always available from
http://hibernate.org, under the
left-hand menu link named “Download.” Various older versions and additional libraries are
available from the resulting page, but you should select Hibernate Core 3.2.0 or a later ver-
sion. A
t the time of writing, this is still a release-candidate version, but we expect the final
release to be available by the time you read this book—if it is not, and you don’t want to use
a pre-release version, then most of the examples will work equally well with the previous
3.1.0 r
elease of the Hibernate core. Download the archive and unpack it to a local directory.
The unpacked archive contains all the source code for Hibernate itself, a JAR library built
from this source, and all the library files that are necessary to run the sample.
Y
ou should then download Hibernate Tools from the same site. At the time of writing, it
is currently at version 3.1 (again, this is currently in a late beta release, but we recommend
using the beta version, rather that its inferior predecessors, if a final 3.1 version has not been
released yet). Hibernate Tools provides various plug-ins for the Ant build tool and the free
Eclipse IDE. In this chapter, we make use of the Ant plug-ins only, but we discuss the Eclipse
27
CHAPTER 3
■ ■ ■
6935ch03_final.qxd 8/2/06 9:36 PM Page 27
features in Appendix B. Again, the archive should be downloaded and unpacked to a local
directory. This archive does not include the source code (which is available elsewhere on the
www.hibernate.org site, if you decide to take a look at it).
HSQLDB 1.8.0
The database we will be using in our examples is the HSQL database. This is written in Java
and is freely available open source software. While we used version 1.8.0.2 for our exam-
ples, we expect that any later version will be suitable. HSQL is derived from code originally
released as “Hypersonic.” You may encounter the term in some of the HSQL documenta-
tion and should treat it as synonymous with “HSQL.” We may also refer to the product as
HSQLDB when it might otherwise be mistaken for Hibernate Query Language (HQL),
whose acronym is distressingly similar!
Our examples are tailored to HSQL because HSQL will run on any of the platforms that
Hibernate will run on, and because HSQL is freely available with minimal installation require-
ments. However, if you want to run the examples with your own database, then the differences
should boil down to the following:
• The Hibernate dialect class
• The JDBC driver
• The connection URL for the database
• The username for the database
• The password for the database
You will see where these can be specified later in this chapter. You will notice that where
we specify the URL for connection to the database, we often append a
shutdown=true attrib-
ute. This fixes a minor problem in which HSQLDB does not write its changes to disk until a
Connection object is closed (something that may never happen when a connection is being
managed by Hibernate’s own connection pooling logic). This is not necessary on non-
embedded databases.
Ant 1.6.5
Y
ou will want to install the Ant build tool.
W
e will not attempt to explain the
build.xml for
mat
in detail; if you are familiar with Ant, then the example build script provided in this chapter will
be enough to get you started—if not, then Ant is a topic in its own right. We would recommend
E
nterprise Jav
a D
evelopment on a Budget
, b
y Christopher M. J
udd and Brian Sam-Bodden
(Apress, 2004), for good coverage of open source tools such as Ant.
While Ant in general lies outside the scope of this book, we will discuss the use of the
H
ibernate tasks used b
y our scripts.
Listing 3-1 provides the Ant script to build the example for this chapter.
CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION28
6935ch03_final.qxd 8/2/06 9:36 PM Page 28
Listing 3-1. An Ant Script to Build the Chapter 3 Examples
<
project name="sample">
<property file="build.properties"/>
<property name="src" location="src"/>
<property name="bin" location="bin"/>
<property name="sql" location="sql"/>
<property name="hibernate.tools"
value="${hibernate.tools.home}${hibernate.tools.path}"/>
<path id="classpath.base">
<pathelement location="${src}"/>
<pathelement location="${bin}"/>
<pathelement location="${hibernate.home}/hibernate3.jar"/>
<fileset dir="${hibernate.home}/lib" includes="**/*.jar"/>
<pathelement location="${hsql.home}/lib/hsqldb.jar"/>
</path>
<path id="classpath.tools">
<path refid="classpath.base"/>
<pathelement
location="${hibernate.tools}/hibernate-tools.jar"/>
</path>
<taskdef name="htools"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="classpath.tools"/>
<target name="exportDDL" depends="compile">
<htools destdir="${sql}">
<classpath refid="classpath.tools"/>
<configuration
configurationfile="${src}/hibernate.cfg.xml"/>
<hbm2ddl drop="true" outputfilename="sample.sql"/>
</htools>
</target>
<target name="compile">
<javac srcdir="${src}" destdir="${bin}" classpathref="classpath.base"/>
</target>
<target name="populateMessages" depends="compile">
<java classname="sample.PopulateMessages" classpathref="classpath.base"/>
</target>
CHAPTER 3 ■ BUILDING A SIMPLE APPLICATION 29
6935ch03_final.qxd 8/2/06 9:36 PM Page 29
剩余37页未读,继续阅读
资源评论
- 梦幻如真2013-07-19只有前三章内容,感觉上当了。。。
- qq2444166192013-04-11讲的比较易懂
萧博士
- 粉丝: 93
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功