Hibernate Recipes (eBook)
XXI, 312 Seiten
Apress (Verlag)
978-1-4302-2797-7 (ISBN)
Hibernate continues to be the most popular out-of-the-box framework solution for Java Persistence and data/database accessibility techniques and patterns. It is used for e-commerce-based web applications as well as heavy-duty transactional systems for the enterprise.
Gary Mak, the author of the best-selling Spring Recipes, now brings you Hibernate Recipes. This book contains a collection of code recipes and templates for learning and building Hibernate solutions for you and your clients.
This book is your pragmatic day-to-day reference and guide for doing all things involving Hibernate. There are many books focused on learning Hibernate, but this book takes you further and shows how you can apply it practically in your daily work.
Gary Mak, founder and chief consultant of Meta-Archit Software Technology Limited, has been a technical architect and application developer on the enterprise Java platform for more than seven years. He is the author of the Apress books Spring Recipes: A Problem-Solution Approach and Pro SpringSource dm Server. In his career, Gary has developed a number of Java-based software projects, most of which are application frameworks, system infrastructures, and software tools. He enjoys designing and implementing the complex parts of software projects. Gary has a master's degree in computer science. His research interests include object-oriented technology, aspect-oriented technology, design patterns, software reuse, and domain-driven development. Gary specializes in building enterprise applications on technologies including Spring, Hibernate, JPA, JSF, Portlet, AJAX, and OSGi. He has been using the Spring Framework in his projects since Spring version 1.0. Gary has been an instructor of courses on enterprise Java, Spring, Hibernate, Web Services, and agile development. He has written a series of Spring and Hibernate tutorials as course materials, parts of which are open to the public, and they're gaining popularity in the Java community. In his spare time, he enjoys playing tennis and watching tennis competitions.
Hibernate continues to be the most popular out-of-the-box framework solution for Java Persistence and data/database accessibility techniques and patterns. It is used for e-commerce-based web applications as well as heavy-duty transactional systems for the enterprise. Gary Mak, the author of the best-selling Spring Recipes, now brings you Hibernate Recipes. This book contains a collection of code recipes and templates for learning and building Hibernate solutions for you and your clients. This book is your pragmatic day-to-day reference and guide for doing all things involving Hibernate. There are many books focused on learning Hibernate, but this book takes you further and shows how you can apply it practically in your daily work.
Gary Mak, founder and chief consultant of Meta-Archit Software Technology Limited, has been a technical architect and application developer on the enterprise Java platform for more than seven years. He is the author of the Apress books Spring Recipes: A Problem-Solution Approach and Pro SpringSource dm Server. In his career, Gary has developed a number of Java-based software projects, most of which are application frameworks, system infrastructures, and software tools. He enjoys designing and implementing the complex parts of software projects. Gary has a master's degree in computer science. His research interests include object-oriented technology, aspect-oriented technology, design patterns, software reuse, and domain-driven development. Gary specializes in building enterprise applications on technologies including Spring, Hibernate, JPA, JSF, Portlet, AJAX, and OSGi. He has been using the Spring Framework in his projects since Spring version 1.0. Gary has been an instructor of courses on enterprise Java, Spring, Hibernate, Web Services, and agile development. He has written a series of Spring and Hibernate tutorials as course materials, parts of which are open to the public, and they're gaining popularity in the Java community. In his spare time, he enjoys playing tennis and watching tennis competitions.
Contents at a Glance 5
Table of Contents 6
About the Authors 20
About the Technical Reviewer 21
Acknowledgments 22
CHAPTER 1 Starting with Hibernate 23
1.1 Setting Up Hibernate 25
Problem 25
Solution 25
How It Works 26
Installing the JDK 26
Installing the Eclipse Web Tools Platform (WTP) 27
Installing Derby 27
1.2 Programming with Basic JDBC 29
Problem 29
Solution 29
How It Works 29
Creating an Eclipse Project 29
JDBC Initialization and Cleanup 30
Using JDBC to Query a Database 30
Using JDBC to Update a Database 30
Creating the Domain Model 31
Retrieving Object Graphs 32
Persisting Object Graphs 33
Problems with Using JDBC 34
1.3 Configuring Hibernate 34
Problem 34
Solution 34
How It Works 34
Getting the Required Jars 35
Creating Mapping Definitions 35
Configuration 36
Programmatc Configuration 36
XML Configuration 37
Opening and Closing Sessions 38
Retrieving Objects 38
1.4 Configuring a JPA Project 39
Problem 39
Solution 39
How It Works 39
Opening a Session 44
1.5 Using Entity Manager 45
Problem 45
Solution 45
How It Works 45
1.6 Enabling Logging in Hibernate 49
Problem 49
Solution 49
How It Works 50
Inspecting the SQL Statements Issued by Hibernate 50
Configuring Log4j 50
Enabling Live Statistics 50
1.7 Generating a Database Schema Using Hibernate 51
Problem 51
Solution 51
How It Works 51
Creating an Ant Build File 51
Generating Database Schema Using SchemaExport 52
Updating a Database Schema Using SchemaUpdate 52
Specifying the Details of a Database Schema 52
Summary 53
CHAPTER 2 Basic Mapping and Object Identity 54
2.1 Providing an ID for Persistence 54
Problem 54
Solution 54
How It Works 54
Hibernate XML Mapping 55
Using the Database Sequence 56
Using a Native Generator 56
Using an Increment Generator 57
Using the Hilo Generator 58
Using JPA to Generate Identifiers 59
2.2 Creating a Composite Key in Hibernate 59
Problem 59
Solution 60
How It Works 60
2.3 SaveOrUpdate in Hibernate 63
Problem 63
Solution 63
How It Works 64
2.4 Dynamic SQL Generation in Hibernate 64
Problem 64
Solution 65
How It Works 65
2.5 Naming Entities in Hibernate 66
Problem 66
Solution 66
How It Works 67
Summary 69
CHAPTER 3 Component Mapping 70
3.1 Implementing a Value Type as a Component 70
Problem 70
Solution 70
How It Works 70
Using JPA Annotations 73
3.2 Nesting Components 76
Problem 76
Solution 76
How It Works 77
3.3 Adding References in Components 79
Problem 79
Solution 79
How It Works 79
3.4 Mapping a Collection of Components 82
Problem 82
Solution 82
How It Works 82
3.5 Using Components as Keys to a Map 87
Problem 87
Solution 87
How It Works 87
Summary 88
CHAPTER 4 Inheritance and Custom Mapping 89
4.1 Mapping Entities with Table per Class Hierarchy 90
Problem 90
Solution 90
How It Works 92
4.2 Mapping Entities with Table per Subclass 94
Problem 94
Solution 95
How It Works 95
4.3 Mapping Entities with Table per Concrete Class 98
Problem 98
Solution 98
How It Works 98
4.4 Custom Mappings 101
Problem 101
Solution 101
How It Works 101
4.5 CompositeUserType Mappings 107
Problem 107
Solution 107
How It Works 107
Summary 113
CHAPTER 5 Many-to-One and One-to-One Mapping 115
5.1 Using Many-To-One Associations 115
Problem 115
Solution 116
How It Works 116
5.2 Using a Many-to-One Association with a Join Table 119
Problem 119
Solution 120
How It Works 120
5.3 Using Lazy Initialization on Many-to-One Associations 122
Problem 122
Solution 122
How It Works 123
5.4 Sharing Primary Key Associations 124
Problem 124
Solution 124
How It Works 124
5.5 Creating a One-to-One Association Using a Foreign Key 127
Problem 127
Solution 127
How It Work 127
5.6 Creating a One-to-One Association Using a Join Table 129
Problem 129
Solution 129
How It Works 129
Summary 133
CHAPTER 6 Collection Mapping 134
6.1 Mapping a Set 134
Problem 134
Solution 134
How It Works 135
6.2 Mapping a Bag 137
Problem 137
Solution 137
How It Works 137
6.3 Mapping a List 141
Problem 141
Solution 141
How It Works 141
6.4 Mapping an Array 143
Problem 143
Solution 143
How It Works 143
6.5 Mapping a Map 145
Problem 145
Solution 145
How It Works 145
6.6 Sorting Collections 147
Problem 147
Solution 147
How It Works 148
Using the Natural Order 148
Writing Your Own Comparator 149
Sorting in the Database 151
6.6 Using Lazy Initialization 152
Problem 152
Solution 152
How It Works 153
Summary 154
CHAPTER 7 Many-Valued Associations 155
7.1 Mapping a One-to-Many Association with a Foreign Key 155
Problem 155
Solution 155
How It Works 156
7.2 Mapping a One-to-Many Bidirectional Association Using a Foreign Key 160
Problem 160
Solution 160
How It Works 161
7.3 Mapping a One-to-Many Bidirectional Association Using a Join Table 163
Problem 163
Solution 163
How It Works 163
7.4 Mapping a Many-to-Many Unidirectional Association with a Join Table 166
Problem 166
Solution 167
How It Works 167
7.5 Creating a Many-to-Many Bidirectional Association with a Join Table 168
Problem 168
Solution 168
How It Works 169
Summary 171
CHAPTER 8 HQL and JPA Query Language 172
8.1 Using the Query Object 172
Problem 172
Solution 172
How It Works 173
Creating a Query Object 173
The from Clause 173
The where Clause 174
Pagination 174
Parameter Binding 175
Named Queries 177
8.2 Using the Select Clause 178
Problem 178
Solution 178
How It Works 178
8.3 Joining 180
Problem 180
Solution 180
How It Works 180
Explicit Joins 180
Implicit Joins 181
Outer Joins 181
Matching Text 181
Fetching Associations 182
8.4 Creating Report Queries 182
Problem 182
Solution 182
How It Works 182
Projection with Aggregation Functions 182
Grouping Aggregated Results 183
Summary 183
CHAPTER 9 Querying with Criteria and Example 184
9.1 Using Criteria 185
Problem 185
Solution 185
How It Works 185
9.2 Using Restrictions 186
Problem 186
Solution 186
How It Works 186
Writing Subqueries 188
9.3 Using Criteria in Associations 189
Problem 189
Solution 189
How It Works 189
9.4 Using Projections 191
Problem 191
Solution 191
How It Works 191
Aggregate Functions and Groupings with Projections 192
9.5 Querying by Example 193
Problem 193
Solution 193
How It Works 193
Summary 194
CHAPTER 10 Working with Objects 195
10.1 Identifying Persistent Object States 195
Problem 195
Solution 195
How It Works 195
Transient Objects 195
Persistent Objects 196
Detached Objects 196
Removed Objects 197
10.2 Working with Persistent Objects 198
Problem 198
Solution 198
How It Works 198
Creating a Persistent Object 198
Retrieving a Persistent Object 200
Modifying a Persistent Object 201
Deleting a Persistent Object 201
10.3 Persisting Detached Objects 202
Problem 202
Solution 202
How It Works 202
Reattaching a Detached Object 202
Merging a Detached Object 202
10.4 Using Data Filters 203
Problem 203
Solution 203
How It Works 204
10.5 Using Interceptors 206
Problem 206
Solution 206
How It Works 206
Summary 208
CHAPTER 11 Batch Processing and Native SQL 209
11.1 Performing Batch Inserts 210
Problem 210
Solution 210
How It Works 210
11.2 Performing Batch Updates and Deletes 211
Problem 211
Solution 211
How It Works 211
11.3 Using Native SQL 213
Problem 213
Solution 213
How It Works 214
11.4 Using Named SQL Queries 215
Problem 215
Solution 215
How It Works 215
Summary 217
CHAPTER 12 Cashing in Hibernate 218
Using the Second-Level Cache in Hibernate 219
Concurrency Strategies 220
Cache Providers 220
What Are Cache Regions? 222
Caching Query Results 222
12.1 Using the First-Level Cache 222
Problem 222
Solution 223
How It Works 223
12.2 Configuring the Second-Level Cache 224
Problem 224
Solution 224
How It Works 224
12.3 Caching Associations 227
Problem 227
Solution 227
How It Works 227
12.4 Caching Collections 228
Problem 228
Solution 228
How It Works 228
12.5 Caching Queries 230
Problem 230
Solution 230
How It Works 230
Summary 232
CHAPTER 13 Transactions and Concurrency 233
13.1 Using Programmatic Transactions in a Standalone Java Application 234
Problem 234
Solution 234
How It Works 235
13.2 Using Programmatic Transactions with JTA 237
Problem 237
Solution 237
How It Works 238
13.3 Enabling Optimistic Concurrency Control 242
Problem 242
Solution 242
How It Works 245
13.4 Using Pessimistic Concurrency Control 248
Problem 248
Solution 248
How It Works 249
Summary 250
CHAPTER 14 Web Applications 251
14.1 Creating a Controller for the Bookshop Web Application 252
Problem 252
Solution 252
How It Works 252
Creating a Dynamic Web Project 252
Configuring the Connection Pool 255
Developing an Online Bookshop 256
Creating a Global Session Factory 256
Listing Persistent Objects 256
Updating Persistent Objects 258
Creating Persistent Objects 263
Deleting Persistent Objects 266
14.2 Creating a Data-Access Layer 268
Problem 268
Solution 268
How It Works 269
Organizing Data Access in Data-Access Objects 269
Using Generic Data-Access Objects 271
Using a Factory to Centralize DAO Retrieval 273
Navigating Lazy Associations 275
Using the Open Session in View Pattern 276
Summary 278
Index 279
Erscheint lt. Verlag | 11.8.2010 |
---|---|
Zusatzinfo | XXI, 312 p. |
Verlagsort | Berkeley |
Sprache | englisch |
Themenwelt | Mathematik / Informatik ► Informatik ► Programmiersprachen / -werkzeuge |
Informatik ► Software Entwicklung ► Objektorientierung | |
Mathematik / Informatik ► Informatik ► Theorie / Studium | |
Mathematik / Informatik ► Informatik ► Web / Internet | |
Schlagworte | Database • Hibernate • Java • object • Processing • Spring Framework • SQL |
ISBN-10 | 1-4302-2797-4 / 1430227974 |
ISBN-13 | 978-1-4302-2797-7 / 9781430227977 |
Haben Sie eine Frage zum Produkt? |
Größe: 5,3 MB
DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasserzeichen und ist damit für Sie personalisiert. Bei einer missbräuchlichen Weitergabe des eBooks an Dritte ist eine Rückverfolgung an die Quelle möglich.
Dateiformat: PDF (Portable Document Format)
Mit einem festen Seitenlayout eignet sich die PDF besonders für Fachbücher mit Spalten, Tabellen und Abbildungen. Eine PDF kann auf fast allen Geräten angezeigt werden, ist aber für kleine Displays (Smartphone, eReader) nur eingeschränkt geeignet.
Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.
Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.
aus dem Bereich