1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Beginning Hibernate 2nd Edition doc

401 1,5K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 401
Dung lượng 9,59 MB

Nội dung

Linwood Minter SECOND EDITION Hibernate Companion eBook Available Beginning this print for content only—size & color not accurate   CYAN   MAGENTA   YELLOW   BLACK   PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® US $44.99 Shelve in: Programming Languages/Java User level: Beginner-Intermediate Dave Minter, Author of Beginning Spring 2 Coauthor of Beginning Hibernate, 1st Edition Building Portals with the Java TM Portlet API Pro Hibernate 3 THE APRESS ROADMAP The Definitive Guide to Terracotta Spring Persistence with Hibernate Hibernate Recipes Spring Enterprise Framework Beginning Hibernate www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN 978-1-4302-2850-9 9 781430 228509 5 44 9 9 Jeff Linwood, Coauthor of Beginning Hibernate, 1st Edition Professional Struts Applications Building Portals with the Java TM Portlet API Pro Hibernate 3 Beginning Hibernate Dear Reader, Hibernate 3.5 lets you save normal Java™ objects into a relational database, and retrieve them without having to write a line of SQL. It makes database per- sistence as natural to use as other Java™ libraries. More and more companies are using Hibernate in their systems. In our view, this trend is inevitable and unstoppable—once you have successfully shipped a project with Hibernate, there is no going back. Hibernate’s support for and influence over the JPA™ 2 specification gives it tremendous credibility in any organization that prefers standards to proprietary solutions. Beginning Hibernate, Second Edition is your introduction to this wonderful world of database persistence. Our aim is to give you a firm understanding of the basic features of Hibernate, such as annotating your Plain Old Java Objects and querying databases. With this understanding of the fundamental features, you will then be in a great position to take advantage of the more advanced or obscure features that we discuss in later chapters and appendixes, including the use of the Hibernate plug-ins for Eclipse and Ant, execution of advanced queries, and the integration of Hibernate with the Spring API. Hibernate is definitely a great product, but it is not a simple one. We take a pragmatic view of the benefits of tools, and we believe that the best way to learn any new tool is to use it. To that end, we have provided lots of simple work- ing examples of all the features we describe. We believe that you will find in this book everything that you need to build a fully functional Hibernate-based application, and to become a Hibernate aficionado. We congratulate you on your choice of Hibernate, and wish you luck in all your endeavors. Jeff Linwood and Dave Minter 7.5 x 9.25 spine = 0.75" 400 page count Beginning Hibernate SECOND EDITION Jeff Linwood and Dave Minter An introduction to persistence using Hibernate 3.5 Covers Hibernate 3.5 THE EXPERT’S VOICE ®  IN JAVA TM  TECHNOLOGY Covers Hilbernate 3.5 www.it-ebooks.info www.it-ebooks.info Beginning Hibernate Second Edition ■ ■ ■ Jeff Linwood and Dave Minter www.it-ebooks.info Beginning Hibernate, Second Edition Copyright © 2010 by Jeff Linwood and Dave Minter 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-4302-2850-9 ISBN-13 (electronic): 978-1-4302-2851-6 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. President and Publisher: Paul Manning Lead Editor: Jonathan Gennick Technical Reviewer: Sumit Pal Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Debra Kelly Copy Editor: Sharon Terdeman Production Support: Patrick Cunningham Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko 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 e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. 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. www.it-ebooks.info iii Contents at a Glance About the Authors xv About the Technical Reviewer xvi Acknowledgments xvii Introduction xviii ■Chapter 1: An Introduction to Hibernate 3.5 1 ■Chapter 2: Integrating and Configuring Hibernate 9 ■Chapter 3: Building a Simple Application 27 ■Chapter 4: The Persistence Life Cycle 61 ■Chapter 5: An Overview of Mapping 77 ■Chapter 6: Mapping with Annotations 91 ■Chapter 7: Creating Mappings with Hibernate XML Files 133 ■Chapter 8: Using the Session 177 ■Chapter 9: Searches and Queries 193 ■Chapter 10: Advanced Queries Using Criteria 215 ■Chapter 11: Filtering the Results of Searches 227 ■Chapter 12: Case Study – Using Hibernate with an Existing Database 235 www.it-ebooks.info ■ CONTENTS AT A GLANCE iv ■Appendix A: More Advanced Features 263 ■Appendix B: Hibernate Tools 287 ■Appendix C: Hibernate and Spring 327 ■Appendix D: Upgrading from Hibernate 2 337 Index 343 www.it-ebooks.info v Contents About the Authors xv About the Technical Reviewer xvi Acknowledgments xvii Introduction xviii ■Chapter 1: An Introduction to Hibernate 3.5 1 Plain Old Java Objects (POJOs) 1 Origins of Hibernate and Object-Relational Mapping 3 Hibernate As a Persistence Solution 4 A Hibernate Hello World Example 5 Mappings 6 Summary 7 ■Chapter 2: Integrating and Configuring Hibernate 9 The Steps Needed to Integrate and Configure Hibernate 9 Understanding Where Hibernate Fits in Your Java Application 10 Deploying Hibernate 11 Required Libraries for Running Hibernate 3.5 11 JMX and Hibernate 12 www.it-ebooks.info ■ CONTENTS vi Hibernate Configuration 12 Hibernate Properties 14 XML Configuration 18 Annotated Classes 19 Naming Strategy 20 Using a Container-Managed Data Source 21 The Session Factory 21 SQL Dialects 22 Summary 25 ■Chapter 3: Building a Simple Application 27 Installing the Tools 27 Hibernate and Hibernate Tools 27 Simple Logging Facade for Java (SLF4J) 1.5.11 28 HSQLDB 2.0.0 28 Ant 1.8.0 29 The Ant Tasks 32 Creating a Hibernate Configuration File 33 Running the Message Example 35 Persisting Multiple Objects 38 Creating Persistence Classes 39 Creating the Object Mappings 42 Creating the Tables 48 Sessions 50 The Session and Related Objects 50 Using the Session 52 The Example Client 54 Summary 59 www.it-ebooks.info ■ CONTENTS vii ■Chapter 4: The Persistence Life Cycle 61 Introduction to the Life Cycle 61 Entities, Classes, and Names 62 Identifiers 63 Entities and Associations 63 Saving Entities 67 Object Equality and Identity 68 Loading Entities 69 Refreshing Entities 70 Updating Entities 71 Deleting Entities 72 Cascading Operations 72 Lazy Loading, Proxies, and Collection Wrappers 74 Querying Objects 75 Summary 75 ■Chapter 5: An Overview of Mapping 77 Why Mapping Cannot Be Automated 78 Primary Keys 80 Lazy Loading 82 Associations 82 The One-to-One Association 84 The One-to-Many and Many-to-One Association 86 The Many-to-Many Association 88 Applying Mappings to Associations 89 www.it-ebooks.info ■ CONTENTS viii Other Information Represented in Mappings 89 Specification of (Database) Column Types and Sizes 89 The Mapping of Inheritance Relationships to the Database 89 Primary Key 90 The Use of SQL Formula–Based Properties 90 Mandatory and Unique Constraints 90 Cascading of Operations 90 Summary 90 ■Chapter 6: Mapping with Annotations 91 Creating Hibernate Mappings with Annotations 91 Cons of Annotations 91 Pros of Annotations 91 Choosing Which to Use 93 Using Annotations in Your Application 93 JPA 2 Persistence Annotations 93 Entity Beans with @Entity 95 Primary Keys with @Id and @GeneratedValue 95 Generating Primary Key Values with @SequenceGenerator 97 Generating Primary Key Values with @TableGenerator 98 Compound Primary Keys with @Id, @IdClass, or @EmbeddedId 99 Database Table Mapping with @Table and @SecondaryTable 103 Persisting Basic Types with @Basic 105 Omitting Persistence with @Transient 105 Mapping Properties and Fields with @Column 106 Modeling Entity Relationships 107 Inheritance 114 Other JPA 2 Persistence Annotations 117 Ordering Collections with @OrderColumn 119 Configuring the Annotated Classes 121 www.it-ebooks.info [...]... you see fit The Hibernate developers provided implementations of the core Hibernate 2 objects in Hibernate 3.5 with the Hibernate 2 methods for backward compatibility We discuss the differences between Hibernate 2 and Hibernate 3.5 in more depth in Appendix D One of the key features of Hibernate s design is the principle of least intrusiveness—the Hibernate developers did not want Hibernate to intrude... need to write the logic in your application that uses the Hibernate session to actually do something! But once you learn how to integrate Hibernate with your application, the basics apply for any project that uses Hibernate If you already have an application that uses Hibernate 2, the migration path from Hibernate 2 to Hibernate 3.5 is easy While Hibernate 3.5 is not completely backward-compatible, most... Using JMX, you can configure Hibernate while it is running Hibernate may be deployed as a JCA connector, and you can use JNDI to obtain a Hibernate session factory in your application In addition, Hibernate uses standard Java Database Connectivity (JDBC) database drivers to access the relational database Hibernate does not replace JDBC as a database connectivity layer Hibernate sits on a level above... next step is to deploy hibernate3 .jar with your application This JAR file is provided with the Hibernate 3.5 binary distribution The file contains the classes in the org .hibernate package, along with several DTD and XML Schema files You will then need to deploy the other required libraries Required Libraries for Running Hibernate 3.5 Hibernate requires several libraries beyond hibernate3 .jar These libraries... versions of Hibernate 11 www.it-ebooks.info CHAPTER 2 ■ INTEGRATING AND CONFIGURING HIBERNATE There are several optional libraries included with the Hibernate 3.5 distribution If you build Hibernate from source, a few of these are necessary for Hibernate to compile Other libraries provide connection pools, additional caching functionality (the Session cache is mandatory), and the JCA API JMX and Hibernate. .. for services and resources Hibernate provides two MBeans for JMX: HibernateServiceMBean and StatisticsServiceMBean Both of these are interfaces that reside in the org .hibernate. jmx package The HibernateService and StatisticsService classes implement the interfaces and reside within the same package The HibernateServiceMBean provides getter and setter methods for many of the Hibernate configuration properties,... the Session object 13 www.it-ebooks.info CHAPTER 2 ■ INTEGRATING AND CONFIGURING HIBERNATE Hibernate Properties Typically, you will specify your Hibernate configuration in a properties file called hibernate. properties in the root directory of your application’s classpath, or as identified values in a hibernate. cfg.xml file Hibernate has an extensive list of properties for configuration (see Table 2-1)... turns on SQL logging statements for debugging: hibernate. connection.driver_class = org.hsqldb.jdbcDriver hibernate. connection.url = jdbc:hsqldb:file:exampledb;shutdown=true hibernate. connection.username = admin hibernate. connection.password = password hibernate. dialect = org .hibernate. dialect.HSQLDialect hibernate. show_sql = true Unless you provide a JDBC connection programmatically in your application,... can enforce the proper transactional boundaries when using Hibernate Appendix D discusses some topics of interest to developers who are working with a preexisting base of code that was built using version 2 of Hibernate We present the various approaches for coexisting with Hibernate 3 code, as well as for migrating a Hibernate 2 code base to the Hibernate 3 API Downloading the Code The source code for... are included in the lib/required directory of your Hibernate 3.5 installation Besides the libraries in lib/required, Hibernate also uses a JPA library, which is included in the lib/jpa directory Hibernate 3.5 also requires a bytecode library to function There are two bytecode libraries shipped with the Hibernate distribution – javassist and CGLib With Hibernate 3.5, you need to use one or the other In . Coauthor of Beginning Hibernate, 1st Edition Professional Struts Applications Building Portals with the Java TM Portlet API Pro Hibernate 3 Beginning Hibernate Dear. Minter, Author of Beginning Spring 2 Coauthor of Beginning Hibernate, 1st Edition Building Portals with the Java TM Portlet API Pro Hibernate 3 THE APRESS

Ngày đăng: 17/02/2014, 23:20

TỪ KHÓA LIÊN QUAN