E-Book, Englisch, 312 Seiten
Mak / Guruzu Hibernate Recipes
1. ed
ISBN: 978-1-4302-2797-7
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark
A Problem-Solution Approach
E-Book, Englisch, 312 Seiten
ISBN: 978-1-4302-2797-7
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark
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.
Autoren/Hrsg.
Weitere Infos & Material
1;Contents at a Glance;5
2;Table of Contents;6
3;About the Authors;20
4;About the Technical Reviewer;21
5;Acknowledgments;22
6;CHAPTER 1 Starting with Hibernate;23
6.1;1.1 Setting Up Hibernate;25
6.2;Problem;25
6.3;Solution;25
6.4;How It Works;26
6.4.1;Installing the JDK;26
6.4.2;Installing the Eclipse Web Tools Platform (WTP);27
6.4.3;Installing Derby;27
6.5;1.2 Programming with Basic JDBC;29
6.5.1;Problem;29
6.5.2;Solution;29
6.5.3;How It Works;29
6.5.3.1;Creating an Eclipse Project;29
6.5.3.2;JDBC Initialization and Cleanup;30
6.5.3.3;Using JDBC to Query a Database;30
6.5.3.4;Using JDBC to Update a Database;30
6.5.3.5;Creating the Domain Model;31
6.5.3.6;Retrieving Object Graphs;32
6.5.3.7;Persisting Object Graphs;33
6.5.3.8;Problems with Using JDBC;34
6.6;1.3 Configuring Hibernate;34
6.6.1;Problem;34
6.6.2;Solution;34
6.6.3;How It Works;34
6.6.3.1;Getting the Required Jars;35
6.6.3.2;Creating Mapping Definitions;35
6.6.3.3;Configuration;36
6.6.3.4;Programmatc Configuration;36
6.6.3.5;XML Configuration;37
6.6.3.6;Opening and Closing Sessions;38
6.6.3.7;Retrieving Objects;38
6.7;1.4 Configuring a JPA Project;39
6.7.1;Problem;39
6.7.2;Solution;39
6.7.3;How It Works;39
6.7.3.1;Opening a Session;44
6.8;1.5 Using Entity Manager;45
6.8.1;Problem;45
6.8.2;Solution;45
6.8.3;How It Works;45
6.9;1.6 Enabling Logging in Hibernate;49
6.9.1;Problem;49
6.9.2;Solution;49
6.9.3;How It Works;50
6.9.3.1;Inspecting the SQL Statements Issued by Hibernate;50
6.9.3.2;Configuring Log4j;50
6.9.3.3;Enabling Live Statistics;50
6.10;1.7 Generating a Database Schema Using Hibernate;51
6.10.1;Problem;51
6.10.2;Solution;51
6.10.3;How It Works;51
6.10.3.1;Creating an Ant Build File;51
6.10.3.2;Generating Database Schema Using SchemaExport;52
6.10.3.3;Updating a Database Schema Using SchemaUpdate;52
6.10.3.4;Specifying the Details of a Database Schema;52
6.11;Summary;53
7;CHAPTER 2 Basic Mapping and Object Identity;54
7.1;2.1 Providing an ID for Persistence;54
7.1.1;Problem;54
7.1.2;Solution;54
7.1.3;How It Works;54
7.1.3.1;Hibernate XML Mapping;55
7.1.3.1.1;Using the Database Sequence;56
7.1.3.1.2;Using a Native Generator;56
7.1.3.1.3;Using an Increment Generator;57
7.1.3.1.4;Using the Hilo Generator;58
7.1.3.2;Using JPA to Generate Identifiers;59
7.2;2.2 Creating a Composite Key in Hibernate;59
7.2.1;Problem;59
7.2.2;Solution;60
7.2.3;How It Works;60
7.3;2.3 SaveOrUpdate in Hibernate;63
7.3.1;Problem;63
7.3.2;Solution;63
7.3.3;How It Works;64
7.4;2.4 Dynamic SQL Generation in Hibernate;64
7.4.1;Problem;64
7.4.2;Solution;65
7.4.3;How It Works;65
7.5;2.5 Naming Entities in Hibernate;66
7.5.1;Problem;66
7.5.2;Solution;66
7.5.3;How It Works;67
7.6;Summary;69
8;CHAPTER 3 Component Mapping;70
8.1;3.1 Implementing a Value Type as a Component;70
8.1.1;Problem;70
8.1.2;Solution;70
8.1.3;How It Works;70
8.1.4;Using JPA Annotations;73
8.2;3.2 Nesting Components;76
8.2.1;Problem;76
8.2.2;Solution;76
8.2.3;How It Works;77
8.3;3.3 Adding References in Components;79
8.3.1;Problem;79
8.3.2;Solution;79
8.3.3;How It Works;79
8.4;3.4 Mapping a Collection of Components;82
8.4.1;Problem;82
8.4.2;Solution;82
8.4.3;How It Works;82
8.5;3.5 Using Components as Keys to a Map;87
8.5.1;Problem;87
8.5.2;Solution;87
8.5.3;How It Works;87
8.6;Summary;88
9;CHAPTER 4 Inheritance and Custom Mapping;89
9.1;4.1 Mapping Entities with Table per Class Hierarchy;90
9.1.1;Problem;90
9.1.2;Solution;90
9.1.3;How It Works;92
9.2;4.2 Mapping Entities with Table per Subclass;94
9.2.1;Problem;94
9.2.2;Solution;95
9.2.3;How It Works;95
9.3;4.3 Mapping Entities with Table per Concrete Class;98
9.3.1;Problem;98
9.3.2;Solution;98
9.3.3;How It Works;98
9.4;4.4 Custom Mappings;101
9.4.1;Problem;101
9.4.2;Solution;101
9.4.3;How It Works;101
9.5;4.5 CompositeUserType Mappings;107
9.5.1;Problem;107
9.5.2;Solution;107
9.5.3;How It Works;107
9.6;Summary;113
10;CHAPTER 5 Many-to-One and One-to-One Mapping;115
10.1;5.1 Using Many-To-One Associations;115
10.1.1;Problem;115
10.1.2;Solution;116
10.1.3;How It Works;116
10.2;5.2 Using a Many-to-One Association with a Join Table;119
10.2.1;Problem;119
10.2.2;Solution;120
10.2.3;How It Works;120
10.3;5.3 Using Lazy Initialization on Many-to-One Associations;122
10.3.1;Problem;122
10.3.2;Solution;122
10.3.3;How It Works;123
10.4;5.4 Sharing Primary Key Associations;124
10.4.1;Problem;124
10.4.2;Solution;124
10.4.3;How It Works;124
10.5;5.5 Creating a One-to-One Association Using a Foreign Key;127
10.5.1;Problem;127
10.5.2;Solution;127
10.5.3;How It Work;127
10.6;5.6 Creating a One-to-One Association Using a Join Table;129
10.6.1;Problem;129
10.6.2;Solution;129
10.6.3;How It Works;129
10.7;Summary;133
11;CHAPTER 6 Collection Mapping;134
11.1;6.1 Mapping a Set;134
11.1.1;Problem;134
11.1.2;Solution;134
11.1.3;How It Works;135
11.2;6.2 Mapping a Bag;137
11.2.1;Problem;137
11.2.2;Solution;137
11.2.3;How It Works;137
11.3;6.3 Mapping a List;141
11.3.1;Problem;141
11.3.2;Solution;141
11.3.3;How It Works;141
11.4;6.4 Mapping an Array;143
11.4.1;Problem;143
11.4.2;Solution;143
11.4.3;How It Works;143
11.5;6.5 Mapping a Map;145
11.5.1;Problem;145
11.5.2;Solution;145
11.5.3;How It Works;145
11.6;6.6 Sorting Collections;147
11.6.1;Problem;147
11.6.2;Solution;147
11.6.3;How It Works;148
11.6.3.1;Using the Natural Order;148
11.6.3.2;Writing Your Own Comparator;149
11.6.3.3;Sorting in the Database;151
11.7;6.6 Using Lazy Initialization;152
11.7.1;Problem;152
11.7.2;Solution;152
11.7.3;How It Works;153
11.8;Summary;154
12;CHAPTER 7 Many-Valued Associations;155
12.1;7.1 Mapping a One-to-Many Association with a Foreign Key;155
12.1.1;Problem;155
12.1.2;Solution;155
12.1.3;How It Works;156
12.2;7.2 Mapping a One-to-Many Bidirectional Association Using a Foreign Key;160
12.2.1;Problem;160
12.2.2;Solution;160
12.2.3;How It Works;161
12.3;7.3 Mapping a One-to-Many Bidirectional Association Using a Join Table;163
12.3.1;Problem;163
12.3.2;Solution;163
12.3.3;How It Works;163
12.4;7.4 Mapping a Many-to-Many Unidirectional Association with a Join Table;166
12.4.1;Problem;166
12.4.2;Solution;167
12.4.3;How It Works;167
12.5;7.5 Creating a Many-to-Many Bidirectional Association with a Join Table;168
12.5.1;Problem;168
12.5.2;Solution;168
12.5.3;How It Works;169
12.6;Summary;171
13;CHAPTER 8 HQL and JPA Query Language;172
13.1;8.1 Using the Query Object;172
13.1.1;Problem;172
13.1.2;Solution;172
13.1.3;How It Works;173
13.1.3.1;Creating a Query Object;173
13.1.3.2;The from Clause;173
13.1.3.3;The where Clause;174
13.1.3.4;Pagination;174
13.1.3.5;Parameter Binding;175
13.1.3.6;Named Queries;177
13.2;8.2 Using the Select Clause;178
13.2.1;Problem;178
13.2.2;Solution;178
13.2.3;How It Works;178
13.3;8.3 Joining;180
13.3.1;Problem;180
13.3.2;Solution;180
13.3.3;How It Works;180
13.3.3.1;Explicit Joins;180
13.3.3.2;Implicit Joins;181
13.3.3.3;Outer Joins;181
13.3.3.4;Matching Text;181
13.3.3.5;Fetching Associations;182
13.4;8.4 Creating Report Queries;182
13.4.1;Problem;182
13.4.2;Solution;182
13.4.3;How It Works;182
13.4.3.1;Projection with Aggregation Functions;182
13.4.3.2;Grouping Aggregated Results;183
13.5;Summary;183
14;CHAPTER 9 Querying with Criteria and Example;184
14.1;9.1 Using Criteria;185
14.1.1;Problem;185
14.1.2;Solution;185
14.1.3;How It Works;185
14.2;9.2 Using Restrictions;186
14.2.1;Problem;186
14.2.2;Solution;186
14.2.3;How It Works;186
14.2.3.1;Writing Subqueries;188
14.3;9.3 Using Criteria in Associations;189
14.3.1;Problem;189
14.3.2;Solution;189
14.3.3;How It Works;189
14.4;9.4 Using Projections;191
14.4.1;Problem;191
14.4.2;Solution;191
14.4.3;How It Works;191
14.4.3.1;Aggregate Functions and Groupings with Projections;192
14.5;9.5 Querying by Example;193
14.5.1;Problem;193
14.5.2;Solution;193
14.5.3;How It Works;193
14.6;Summary;194
15;CHAPTER 10 Working with Objects;195
15.1;10.1 Identifying Persistent Object States;195
15.1.1;Problem;195
15.1.2;Solution;195
15.1.3;How It Works;195
15.1.3.1;Transient Objects;195
15.1.3.2;Persistent Objects;196
15.1.3.3;Detached Objects;196
15.1.3.4;Removed Objects;197
15.2;10.2 Working with Persistent Objects;198
15.2.1;Problem;198
15.2.2;Solution;198
15.2.3;How It Works;198
15.2.3.1;Creating a Persistent Object;198
15.2.3.2;Retrieving a Persistent Object;200
15.2.3.3;Modifying a Persistent Object;201
15.2.3.4;Deleting a Persistent Object;201
15.3;10.3 Persisting Detached Objects;202
15.3.1;Problem;202
15.3.2;Solution;202
15.3.3;How It Works;202
15.3.3.1;Reattaching a Detached Object;202
15.3.3.2;Merging a Detached Object;202
15.4;10.4 Using Data Filters;203
15.4.1;Problem;203
15.4.2;Solution;203
15.4.3;How It Works;204
15.5;10.5 Using Interceptors;206
15.5.1;Problem;206
15.5.2;Solution;206
15.5.3;How It Works;206
15.6;Summary;208
16;CHAPTER 11 Batch Processing and Native SQL;209
16.1;11.1 Performing Batch Inserts;210
16.1.1;Problem;210
16.1.2;Solution;210
16.1.3;How It Works;210
16.2;11.2 Performing Batch Updates and Deletes;211
16.2.1;Problem;211
16.2.2;Solution;211
16.2.3;How It Works;211
16.3;11.3 Using Native SQL;213
16.3.1;Problem;213
16.3.2;Solution;213
16.3.3;How It Works;214
16.4;11.4 Using Named SQL Queries;215
16.4.1;Problem;215
16.4.2;Solution;215
16.4.3;How It Works;215
16.5;Summary;217
17;CHAPTER 12 Cashing in Hibernate;218
17.1;Using the Second-Level Cache in Hibernate;219
17.2;Concurrency Strategies;220
17.3;Cache Providers;220
17.4;What Are Cache Regions?;222
17.5;Caching Query Results;222
17.6;12.1 Using the First-Level Cache;222
17.6.1;Problem;222
17.6.2;Solution;223
17.6.3;How It Works;223
17.7;12.2 Configuring the Second-Level Cache;224
17.7.1;Problem;224
17.7.2;Solution;224
17.7.3;How It Works;224
17.8;12.3 Caching Associations;227
17.8.1;Problem;227
17.8.2;Solution;227
17.8.3;How It Works;227
17.9;12.4 Caching Collections;228
17.9.1;Problem;228
17.9.2;Solution;228
17.9.3;How It Works;228
17.10;12.5 Caching Queries;230
17.10.1;Problem;230
17.10.2;Solution;230
17.10.3;How It Works;230
17.11;Summary;232
18;CHAPTER 13 Transactions and Concurrency;233
18.1;13.1 Using Programmatic Transactions in a Standalone Java Application;234
18.1.1;Problem;234
18.1.2;Solution;234
18.1.3;How It Works;235
18.2;13.2 Using Programmatic Transactions with JTA;237
18.2.1;Problem;237
18.2.2;Solution;237
18.2.3;How It Works;238
18.3;13.3 Enabling Optimistic Concurrency Control;242
18.3.1;Problem;242
18.3.2;Solution;242
18.3.3;How It Works;245
18.4;13.4 Using Pessimistic Concurrency Control;248
18.4.1;Problem;248
18.4.2;Solution;248
18.4.3;How It Works;249
18.5;Summary;250
19;CHAPTER 14 Web Applications;251
19.1;14.1 Creating a Controller for the Bookshop Web Application;252
19.1.1;Problem;252
19.1.2;Solution;252
19.1.3;How It Works;252
19.1.3.1;Creating a Dynamic Web Project;252
19.1.3.2;Configuring the Connection Pool;255
19.1.3.3;Developing an Online Bookshop;256
19.1.3.4;Creating a Global Session Factory;256
19.1.3.5;Listing Persistent Objects;256
19.1.3.6;Updating Persistent Objects;258
19.1.3.7;Creating Persistent Objects;263
19.1.3.8;Deleting Persistent Objects;266
19.2;14.2 Creating a Data-Access Layer;268
19.2.1;Problem;268
19.2.2;Solution;268
19.2.3;How It Works;269
19.2.3.1;Organizing Data Access in Data-Access Objects;269
19.2.3.2;Using Generic Data-Access Objects;271
19.2.3.3;Using a Factory to Centralize DAO Retrieval;273
19.2.3.4;Navigating Lazy Associations;275
19.2.3.5;Using the Open Session in View Pattern;276
19.3;Summary;278
20;Index;279




