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
[...]... DynaDTO in a JSE Environment 176 DynaDTO in JBoss 177 Implementing Service Layer for TechConf 180 Message-Driven POJOs 189 Summary 194 ■CHAPTER 6 The Spring Framework 195 Coding to Interfaces ... Rails?) Fortunately for us the elephant went on a diet by staying away from the Sun ;-) Today we can build Java applications with a similar level of agility as that showcased by the scripting language camps POJOs are not a new technology or technique but are a “going back to the basis” philosophy in which we concentrate more on the business functionality and less on the plumbing required to build robust applications... it’s your only choice In this book you’ll learn 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... The battle for the acceptance of open source has been largely fought at the level of the programmer and middle management However, upper management, given the recent impact of Linux on corporations, is beginning to see the many advantages of open source, especially in the area of enterprise Java Organizations seeking to reduce software development expenses have found that open source software (OSS) provides... are factors of great importance to improving an attendee’s experience Providing interactivity and constant feedback ensures that attendees are always in tune with the heartbeat of the conference At the beginning of the conference, attendees need to be checked in and given conference badges Changes or updates to any sessions or presentations need to be communicated effectively in order for attendees to... objects that encapsulate (façade) the complexity of the task and simplify the associations between participating objects The question of whether to model the structure or behavior first is one that many beginning and intermediate modelers deal with during every new project We recommend doing both simultaneously because modeling behavior validates the structural integrity of the model, and well-defined . www.it-ebooks.info
Beginning POJOs
From Novice to Professional
■■■
Brian Sam-Bodden
Sam-Bodden_596-3.
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
Ngày đăng: 23/03/2014, 00:20
Xem thêm: Beginning POJOs docx