Thông tin tài liệu
www.it-ebooks.info
Beginning POJOs
From Novice to Professional
■■■
Brian Sam-Bodden
Sam-Bodden_596-3 FRONT.fm Page i Friday, February 24, 2006 9:59 AM
www.it-ebooks.info
Beginning POJOs: From Novice to Professional
Copyright © 2006 by Brian Sam-Bodden
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-159059-596-1
ISBN-10 (pbk): 1-59059-596-3
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.
Lead Editor: Steve Anglin
Technical Reviewer: Dilip Thomas
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Jason Gilmore,
Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft,
Jim Sumser, Matt Wade
Project Manager: Kylie Johnston
Copy Edit Manager: Nicole LeClerc
Copy Editor: Hastings Hart
Assistant Production Director: Kari Brooks-Copony
Production Editor: Katie Stence
Compositor: Susan Glinert
Proofreader: Lori Bring
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 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 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 www.apress.com in the Source Code section.
Sam-Bodden_596-3 FRONT.fm Page ii Friday, February 24, 2006 9:59 AM
www.it-ebooks.info
I dedicate this book to my wife Anne for her unwavering love and support and to my
two-year-old son Michael for putting a smile on my face every time I felt like quitting.
Sam-Bodden_596-3 FRONT.fm Page iii Friday, February 24, 2006 9:59 AM
www.it-ebooks.info
Sam-Bodden_596-3 FRONT.fm Page iv Friday, February 24, 2006 9:59 AM
www.it-ebooks.info
v
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
■CHAPTER 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■CHAPTER 2 Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
■CHAPTER 3 Building with Ant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
■CHAPTER 4 Object Relational Mapping with Hibernate . . . . . . . . . . . . . . . . . . . . . 87
■CHAPTER 5 Business Services with JBoss . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
■CHAPTER 6 The Spring Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
■CHAPTER 7 Tapestry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
■CHAPTER 8 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
■CHAPTER 9 Continuous Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
■CHAPTER 10 Additional Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Sam-Bodden_596-3 FRONT.fm Page v Friday, February 24, 2006 9:59 AM
www.it-ebooks.info
Sam-Bodden_596-3 FRONT.fm Page vi Friday, February 24, 2006 9:59 AM
www.it-ebooks.info
vii
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
■CHAPTER 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The Java EE Market . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Case Study: The TechConf Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Defining the Stakeholders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
The Business Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
General Application Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Attendees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Presenters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Sponsors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Administrators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Architectural Requirements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Open Issues and Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Open Issues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Design Road Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Finding and Refining Candidate Domain Model Elements . . . . . . . . 13
Use Case Modeling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Modeling Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
The Open Source Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Useful Open Source Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
■CHAPTER 2 Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Installing Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Using Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Sam-Bodden_596-3 FRONT.fm Page vii Friday, February 24, 2006 9:59 AM
www.it-ebooks.info
viii
■CONTENTS
Creating an Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Creating a Java Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Creating a Java Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Running a Java Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Debugging a Java Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Eclipse Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Concurrent Versions System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Understanding CVS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
CVS Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
CVS in Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Database Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
SQL Explorer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Web Development Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
■CHAPTER 3 Building with Ant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Introduction to Ant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Obtaining and Installing Ant. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Ant’s Command-Line Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
A Simple Ant Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
More on Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Target Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Datatypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Case Study: Building TechConf with Ant . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Compiling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Buildfile Reuse with Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Javadoc Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Checking Code Conventions with Checkstyle . . . . . . . . . . . . . . . . . . 73
Generating Source-Code Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Generating Browsable Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Document Generation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Cleaning Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
The All Target . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Eclipse Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Sam-Bodden_596-3 FRONT.fm Page viii Friday, February 24, 2006 9:59 AM
www.it-ebooks.info
■CONTENTS
ix
■CHAPTER 4 Object Relational Mapping with Hibernate . . . . . . . . . . . . . . . 87
Introduction to Hibernate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
How Hibernate Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
The Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
POJO-Driven Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Data Model–Driven Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
HBM-Driven Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
The Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Road Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Hibernate in J2SE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Obtaining Hibernate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Setup for POJO Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Eclipse Setup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Database Setup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Quick Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
POJO Sample Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Mapping (HBM) File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Creating the Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Primary Key Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Saving an Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Retrieving a Single Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Retrieving a Collection of Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Case Study: Mapping the TechConf Domain Model . . . . . . . . . . . . . . . . 118
Conferences Home Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
One-to-Many Mapping Using a Set . . . . . . . . . . . . . . . . . . . . . . . . . 121
Testing Conference and Tracks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Many-to-One: Conferences and Venues . . . . . . . . . . . . . . . . . . . . . 129
Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Inheritance: Table-Per-Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . 130
Inheritance: Table Per Subclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
■CHAPTER 5 Business Services with JBoss . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
JBoss . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
JBoss JEMS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
JBoss AS Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Installing and Configuring JBoss . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Service Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Stateless Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Sam-Bodden_596-3 FRONT.fm Page ix Friday, February 24, 2006 9:59 AM
www.it-ebooks.info
[...]... of all I would like to extend my gratitude to my editor Steve Anglin for pushing me to write and finish the work you’re holding Along with Steve, I would like to send a million thanks to the tireless team at Apress, including project manager Kylie Johnston, production editor Katie Stence, copy editor Hastings Hart, and fellow author and technical reviewer Dilip Thomas I’d also like to thank Joseph Nusairat... about the lightweight frameworks and tools that I use in my day -to- day work and that I only not need but enjoy using Who This Book Is For This book targets beginning to intermediate Java developers looking to build enterprise Web applications with the latest offerings from the open source Java community In this book you’ll explore different approaches to building a Java Web application using a step-by-step... reasons for this is the lack of tools that cover the many aspects of Java EE development It is in this area that open source enterprise Java tools and frameworks are emerging to help bridge the gap Learning how to build enterprise applications with a combination of open source frameworks and tools provides a low-cost, low-risk, ideal prototyping environment in which to master distributed computing... of the systems to serve as the basis for a new line of products tailored to the organization, execution, and maintenance of technology conferences These products are to be sold as a collection of components or subsystems that can be adapted to the particular needs of a customer The separation of roles and functionality needs to be clear in order to have a system that can be easily customized and enhanced... administrators with the ability to create and view the results of speaker evaluations • News: Administrators should have an easy interface to update conference-related news • Booth assignment: Administrators should have the ability to select a booth for a sponsor based on sponsorship level and physical requirements • Conference closing: An interface must be provided for conference administrators to easily... for its hardware than its software, Java was born as a stealth project targeting consumer devices Along came the Web, and Java provided the only way to do anything remotely close to rich animation Applets running on the HotJava Browser showcasing a dancing Duke was my “oh, now that’s cool” moment of 1995 So we set out to write enormously large applets that were slow to run and then the browser wars began... entry barrier into the Java and Java EE worlds by providing choices besides the traditional proprietary offerings The rest of this chapter introduces a real-world example that puts the application of Java EE technologies into clearer perspective It introduces a realistic business problem to be used as the backdrop for the learning process of designing, building, and deploying an enterprise Java system... you are like me and most other programmers, you’ll want to jump right into the code, and you might think that the sooner you start coding the sooner you’ll finish your project Well, for all but the most trivial of applications this is not true; a certain amount of planning has to happen before you write a single line of code Feel free to prototype to your heart’s content, but without design, a stable,... presentations From the collected documents, content must be created and also maintained as the source documents change Aside from the document management needs, facilities must be provided for attendees to register and manage the schedule of events they plan to attend At this time it’s also crucial to provide information in a timely manner to make the process of registering and getting to the conference... to obtain driving directions or information about special conference rates for travel and hotel accommodations Conference The information needs at conference time are crucial to the success of the conference Satisfied attendees are more likely to return the following year Being able to cut through the noise, pinpoint areas of interest, and choose sessions to attend are factors of great importance to . like to extend my gratitude to my editor Steve Anglin for pushing me to write and finish
the work you’re holding. Along with Steve, I would like to send. and tools that I use in my day -to- day work and
that I only not need but enjoy using.
Who This Book Is For
This book targets beginning to intermediate Java
Ngày đăng: 23/03/2014, 00:20
Xem thêm: Beginning Java Objects: From Concepts to Code, Second Edition docx, Beginning Java Objects: From Concepts to Code, Second Edition docx