Creating a Hibernate Mapping File . .... Chapter 3 ▫ Exploring Hibernate O/R
Mapping. 45. Understanding O/R .... The Many-to-Many Association Mapping .
Table of Contents
CONTENTS Chapter 1
Getting Started with Hibernate
1
Introducing Hibernate .................................................................................................. 2 Need for Hibernate ................................................................................................ 5 Features of Hibernate ............................................................................................. 6 Disadvantages of Hibernate .................................................................................... 7 Comparison of Hibernate with JDBC ....................................................................... 7 Enhancements in the Hibernate 3.2 Version ............................................................. 8 Exploring Hibernate Architecture................................................................................... 9 Working of Hibernate ................................................................................................ 13 Downloading and Configuring Hibernate ................................................................... 15 Developing Hibernate Application .............................................................................. 16 Creating a Database Table ................................................................................... 17 Creating a POJO class ......................................................................................... 17 Creating a Hibernate Configuration File ................................................................ 18 Creating a Hibernate Mapping File ....................................................................... 19 Creating ClientJava class ...................................................................................... 20 Displaying the Directory Structure of the Application............................................... 21 Compiling and Running the Application ................................................................ 22 Summary .................................................................................................................. 24
Hibernate 3.2 in Simple Steps The Environment Class ......................................................................................... 43 The Settings Class ................................................................................................ 44 Summary .................................................................................................................. 44
Chapter 3
Exploring Hibernate O/R Mapping
45
Understanding O/R Mapping ..................................................................................... 46 Describing Hibernate Mapping Types ......................................................................... 48 Java Primitive Mapping Types ............................................................................... 49 Date and Time Mapping Types ............................................................................. 51 Large Object (LOB) Mapping Types ....................................................................... 52 Other JDK Mapping Types .................................................................................... 54 Describing Hibernate Mapping Element ...................................................................... 55 The Element ................................................................................... 56 The Element ..................................................................... 56 The Element ........................................................................................... 57 The Element ............................................................................................... 59 The Element .................................................................................... 60 The Element ............................................................................... 61 The Element ............................................................................... 62 The Element ........................................................................................ 62 The Element ................................................................................... 63 The Element ...................................................................................... 64 The Element.................................................................................. 65 The Element ....................................................................................... 66 The Element ............................................................................................. 66 Mapping a POJO Class with Database Table .............................................................. 67 Creating a POJO Class ........................................................................................ 68 Creating a Database Table ................................................................................... 69 Creating a Hibernate Mapping File ....................................................................... 69 Summary .................................................................................................................. 70
Chapter 4
Working with Advance Mapping Configuration
71
Understanding Association Mapping .......................................................................... 72 The One-to-One Association Mapping .................................................................. 72 The Many-to-One Association Mapping ................................................................ 74 The Many-to-Many Association Mapping ............................................................... 76 Understanding Collection Mapping ............................................................................ 77 The Element .............................................................................................. 79
iv
Contents The Element .............................................................................................. 80 The Element ........................................................................................... 80 The Element ............................................................................................ 81 Understanding Inheritance Mapping ........................................................................... 82 Table per Class Hierarchy ..................................................................................... 82 Table per Subclass ............................................................................................... 84 Table per Concrete Class ..................................................................................... 87 Exploring Hibernate Annotations ................................................................................ 89 Working of Hibernate Annotations ........................................................................ 92 Developing a Hibernate Application using Annotations .......................................... 94 Compiling and Running the HibernateAnnotation Application ................................ 98 Summary ................................................................................................................ 100
Chapter 5
Working with Hibernate Query Language
101
Introducing HQL ...................................................................................................... 102 Describing the Workflow of HQL ......................................................................... 103 Listing the Features of HQL ................................................................................. 104 Comparing HQL and SQL .................................................................................. 105 Exploring the HQL Statement.................................................................................... 106 Understanding HQL Clauses .............................................................................. 107 Exploring the HQL Aggregate Functions .............................................................. 111 Describing the HQL Expressions .......................................................................... 114 Describing Associations and Joins ....................................................................... 116 Describing Polymorphic Queries ............................................................................... 117 Describing Sub Queries ........................................................................................... 118 Creating and Executing the HQL Statements ............................................................. 118 Implementing the HQL Statements............................................................................ 120 Creating a POJO class ....................................................................................... 121 Creating Database Tables .................................................................................. 123 Creating the hibernate.cfg.xml File ...................................................................... 124 Creating the Hibernate Mapping File .................................................................. 124 Creating Java Classes ........................................................................................ 125 Displaying the Directory Structure of the Application............................................. 132 Summary ................................................................................................................ 132
Chapter 6
Transaction, Concurrency, and Caching
133
Understanding Transactions ..................................................................................... 134 Transaction States .............................................................................................. 135
v
Hibernate 3.2 in Simple Steps ACID Properties ................................................................................................. 135 Exploring Hibernate Transaction ............................................................................... 135 Configuring Hibernate Transaction ..................................................................... 136 Using the Transaction Interface ........................................................................... 137 Flushing the Session ........................................................................................... 138 Handling Exception ............................................................................................ 140 Exploring the org.hibernate.transaction Package ....................................................... 141 Understanding Concurrency Control......................................................................... 142 Optimistic Concurrency Control .......................................................................... 143 Pessimistic Concurrency Control .......................................................................... 144 Exploring Hibernate Cache Architecture .................................................................... 145 First-Level Cache ................................................................................................ 145 Second-Level Cache ........................................................................................... 146 Implementing Hibernate Transaction ........................................................................ 148 Creating the Hibernate Application ..................................................................... 150 Compiling and Running the HibernateTransaction Application ............................. 157 Summary ................................................................................................................ 159
Chapter 7
Working with Persistent Objects
161
Describing the Life cycle of an Object ....................................................................... 162 The Transient State ............................................................................................. 163 The Persistent State ............................................................................................. 163 The Detached State ............................................................................................ 166 Describing the Persistence context ............................................................................. 168 Exploring Hibernate Persistence Manager API ............................................................ 169 Performing Operations on Objects ........................................................................... 170 Changing the State of an Object ......................................................................... 170 Flushing Persistent Objects .................................................................................. 172 Retrieving Persistent Objects ................................................................................ 173 Modifying Persistent Objects ............................................................................... 174 Updating Persistent Objects ................................................................................ 175 Deleting Persistent Objects .................................................................................. 176 Implementing the States of Objects ........................................................................... 176 Creating a POJO class ....................................................................................... 177 Creating a Database Table ................................................................................. 178 Creating a Hibernate Mapping File ..................................................................... 179 Creating a Hibernate Configuration File .............................................................. 180 Creating Java classes ......................................................................................... 180
vi
Contents Displaying the Directory Structure of the Application............................................. 189 Summary ................................................................................................................ 190
Chapter 8
Working with Hibernate in MyEclipse 7.0
191
Overview of the MyEclipse IDE.................................................................................. 192 Features of MyEclipse 7.0 ......................................................................................... 193 Installing MyEclipse 7.0 ............................................................................................ 193 System Requirements .......................................................................................... 194 Installation Procedure ......................................................................................... 194 Exploring MyEclipse Enterprise Workbench ................................................................ 199 Exploring the Menu Bar ...................................................................................... 201 Exploring Views .................................................................................................. 209 Exploring Perspectives......................................................................................... 218 Developing Hibernate Application in MyEclipse ......................................................... 222 Creating a Database Connection ........................................................................ 223 Creating a Database Table ................................................................................. 226 Creating a Java Application ................................................................................ 230 Adding Hibernate Capability............................................................................... 232 Generating Hibernate Reverse Engineering.......................................................... 237 Creating a Java File ........................................................................................... 241 Running the Hibernate Application ...................................................................... 244 Summary ................................................................................................................ 246