Pro Hibernate and MongoDB Anghel Leonard Apress-
Contents. J About the Author xiii About the Technical Reviewer Acknowledgments Introduction xv xvii xix Chapter 1: Getting Started with Hibernate OGM 1 Features and Expectations 2 Hibernate OGM Architecture 2 Persisting Data 4 Querying Data 8 Get the Hibernate OGM Distribution 9 Getting Hibernate OGM from the Maven Central Repository 10 Getting Hibernate OGM from the Maven Command Line 11 Adding MongoDB Artifacts 14 Getting a Hibernate OGM Distribution Using the NetBeans IDE 16 Getting the Hibernate OGM Distribution Using the Eclipse IDE 19 Obtain the MongoDB Distribution 21 Summary 22 Chapter 2: Hibernate OGM and MongoDB 23 Configuring MongoDB-Hibernate OGM Properties 23 Data Storing Representation 25 Storing Entities 26 Storing Primary Keys 28 Storing Associations 29 vii
< CONTENTS Managing Transactions 35 Managing Queries 35 Summary 35 Chapter 3: Bootstrapping Hibernate OGM 37 Brief Overview of JPA 37 Brief Overview of JTA 39 MongoDB and Transactions 40 Brief Overview of Hibernate Native API 40 Bootstrapping Hibernate OGM Using JPA 41 Bootstrap Hibernate OGM Using Hibernate Native API 44 Hibernate OGM Obsolete Configuration Options 50 Summary 50 Chapter 4: Hibernate OGM at Work 51 General Prerequisites 51 Java SE and MongoDB the Hello World Example 52 Prerequisites 52 Developing 53 Testing 55 Hibernate OGM via Hibernate Native API 55 Hibernate OGM in a Non-JTA Environment (JDBC Transactions, Apache Tomcat 7) 55 Hibernate OGM in a Standalone JTA Environment (JBoss JTA, Apache Tomcat 7) 63 Hibernate OGM in a Built-in JTA Environment (no EJB, GlassFish AS 3) 71 Hibernate OGM in a Built-in JTA Environment (EJB 3/BMT, GlassFish AS 3) 76 Hibernate OGM in a Built-in JTA Environment (EJB 3/CMT, GlassFish AS 3) 80 Hibernate OGM via the Java Persistence API (JPA 2.0) 83 Hibernate OGM in a Built-in JTA Environment (EJB 3, GlassFish AS 3) 83 Hibernate OGM in a Built-in JTA Environment (EJB 3, JBoss AS 7) 88 Hibernate OGM in a Standalone JTA environment (Apache Tomcat 7) 94 viii
CONTENTS Hibernate OGM in a Built-in JTA Environment (JBoss AS 7 and Seam 3 Application) 99 Hibernate OGM in a Built-in JTA Environment (GlassFish 3 and Spring 3 Application) 107 Hibernate OGM in a non-jta Environment (RESOURCEJ.OCAL, Apache Tomcat 7) 113 Synthesis 118 Hibernate OGM via JPA in an EE Container 118 Hibernate OGM via Hibernate Native API in an EE Container 118 Hibernate OGM via JPA in Standalone JTA 119 Hibernate OGM via Hibernate Native API in Standalone JTA 119 Hibernate OGM via JPA in Non-JTA 119 Hibernate OGM via Hibernate Native API in Non-JTA 119 Summary 120 Chapter 5: Hibernate OGM and JPA 2.0 Annotations 121 Java Supported Types 121 Eager and Lazy Loading Considerations 122 Cascadable Operations Considerations 122 Entity Mapping 123 Entity Annotation 123 Old Annotation 124 Embeddedld Annotation 133 IdClass Annotation 135 Table Annotation 137 Column Annotation 138 Temporal Annotation 140 Transient Annotation 142 Embedded and Embeddable Annotations 143 Enumerated Annotation 146 Cacheable Annotation 148 MappedSuperclass Annotation 155 ElemerrtCollection Annotation 158 JPA Lifecycle Events EntityListeners, ExcludeDefaultListeners, ExcludeSuperclassListeners Annotations 163 ix
CONTENTS Version Annotation 171 Access Annotation 176 Associations 179 Direction in Entity Associations 179 Unsupported JPA 2.0 Annotations 202 Summary 203 Chapter 6: Hibernate OGM Querying MongoDB 205 MongoDB Native Query 206 Hibernate OGM and CRUD Operations 211 Hibernate Search and Apache Lucene 214 Common Steps 217 Hibernate Search/Apache Lucene Querying OGM via Native API 222 Hibernate Search/Apache Lucene Querying OGM via JPA 231 Hibernate OGM JP-QL Parser 239 Summary 240 Chapter 7: MongoDB e-commerce Database Model 241 MongoDB E-commerce Database Architecture 242 Model the Categories Collection (categories_c) 243 Model The Products Collection (products_c) 245 Model the Customers Collection (customers_c) 250 Model The Orders Collection (orders_c) 256 Model The Inventory Collection (inventory_c) 263 Summary 267 Chapter 8: MongoDB e-commerce Database Querying 269 Display the Categories of Products 271 Display the Promotional Products 273 Display the Products From a Category 274 Search for a Product by Name 276
* CONTENTS Find a Customer By E-mail And Password 277 Place an Order 278 Check the Inventory 279 Restore the Inventory 280 Considerations for Developing the Admin GUI 281 Summary 282 Chapter 9: Migrate MongoDB Database to Cloud 283 Migrating the MongoDB Database to the MongoHQ Cloud 283 Migrating the MongoDB Database to the MongoLab Cloud 289 Connecting to the MongoHQ or MongoLab Cloud Database 294 Summary 296 Chapter 10: Migrating RafaEShop Application on OpenShift 297 Creating a Free Account on OpenShift 297 Installing the OpenShift RHC Client Tools on Windows 301 Installing Ruby 301 Installing Git 302 Testing Ruby and Git from the Shell 303 Installing the OpenShift Gem 305 Fixing a Known Issue 308 Migrating the RafaEShop Application to OpenShift with JBoss AS 7 308 Monitoring the JBoss AS 7 Log 317 Commit Changes 319 Migrating the RafaEShop Application as a WAR 321 Migrating the RafaEShop Application as a Maven Project 322 Migrating the RafaEShop Application to OpenShift with GlassFish 3 AS 327 Monitoring GlassFish Start/Stop 348 Commit Changes 349 Fixing Known Issues 349 Monitoring the GlassFish Log 350 xi
CONTENTS Test It! 351 Cautions 352 Good To Know 354 Disclaimer 354 Summary 354 Index 355 xii