
Guide to Java Persistence and Hibernate Sebastian Hennebrueder Guide to Java Persistence and Hibernate Sebastian Hennebrueder Table of Contents About the author, book and versions ................................................................................................ viii 1. The author ............................................................................................................................. viii 2. The book ................................................................................................................................ viii 3. Library Versions ...................................................................................................................... ix I. Introduction ........................................................................................................................................ 1 1. Introduction to Hibernate ......................................................................................................... 2 1.1. A first Hibernate example ............................................................................................. 2 1.2. Hibernate basics ........................................................................................................... 19 2. Hibernate Concepts - State of Objects ................................................................................... 21 2.1. The three states of objects ........................................................................................... 21 2.2. Lazy initialization, a Hibernate problem ..................................................................... 23 3. Working with Objects ............................................................................................................. 27 3.1. Java Persistence versus Hibernate ............................................................................... 27 3.2. Hibernate API .............................................................................................................. 27 3.3. EntityManager API ...................................................................................................... 34 4. A more complex example – web application ......................................................................... 40 4.1. Summary ...................................................................................................................... 49 II. Mapping, Queries ........................................................................................................................... 51 5. Basic Mappings ...................................................................................................................... 52 5.1. Annotation versus XML .............................................................................................. 52 5.2. Annotation mapping ..................................................................................................... 53 5.3. XML Mapping ............................................................................................................. 58 6. Primary key mapping ............................................................................................................. 63 6.1. Natural versus Surrogate Ids ....................................................................................... 63 6.2. Assigned Id .................................................................................................................. 64 6.3. Generated with Auto Strategy ..................................................................................... 64 6.4. Other Annotation Strategies ........................................................................................ 64 6.5. Composite Id ................................................................................................................ 65 6.6. Equals and Hashcode ................................................................................................... 68 6.7. Other XML Id tags ...................................................................................................... 72 7. Relation mapping .................................................................................................................... 75 7.1. Selecting between List, Set, Map or array to hold many side ..................................... 75 7.2. Uni- and Bi-directional relations ................................................................................. 84 7.3. Cascading ..................................................................................................................... 86 7.4. 1:1 relation ................................................................................................................... 88 7.5. 1:n ................................................................................................................................ 94 7.6. m:n ............................................................................................................................. 104 7.7. 1:n:1 ........................................................................................................................... 109 7.8. Recursive relation ...................................................................................................... 111 7.9. Typed relation (XML only) ....................................................................................... 113 7.10. Typed relation (annotation workaround) ................................................................. 115 8. Components = Composition mapping .................................................................................. 119 8.1. Composition versus entity relations ........................................................................... 119 8.2. Composed class in one table ..................................................................................... 120 8.3. Composition as set of many classes .......................................................................... 123 8.4. Equals implementation ............................................................................................... 124 8.5. Composition as list of many classes .......................................................................... 125 iii Guide to Java Persistence and Hibernate 8.6. Advanced details ........................................................................................................ 127 8.7. Composition 1:n:1 ...................................................................................................... 127 8.8. Not included mappings .............................................................................................. 129 9. Inheritance ............................................................................................................................. 131 9.1. Use Case .................................................................................................................... 131 9.2. Overview on mapping approaches ............................................................................. 131 9.3. Single Table ............................................................................................................... 134 9.4. Joined Inheritance ...................................................................................................... 137 9.5. Joined Inheritance with Discriminator ....................................................................... 140 9.6. Mixing Single table and Joined ................................................................................. 142 9.7. Union Inheritance ...................................................................................................... 142 9.8. XML Includes ............................................................................................................ 145 9.9. Mapped Super Class .................................................................................................. 147 10. Lob with Oracle and PostgreSQL ...................................................................................... 150 10.1. PostgreSQL .............................................................................................................. 150 10.2. Oracle ....................................................................................................................... 152 11. Querying data ...................................................................................................................... 156 11.1. Useful tools .............................................................................................................. 156 11.2. HQL ......................................................................................................................... 157 11.3. Criteria Queries ........................................................................................................ 165 11.4. Native SQL .............................................................................................................. 170 III. Building applications and Architecture ...................................................................................... 176 12. Data Access Objects ........................................................................................................... 177 12.1. Best practices and DAO .......................................................................................... 177 12.2. Data Access Objects DAO ...................................................................................... 177 12.3.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages258 Page
-
File Size-