Pro Hibernate and MongoDB.pdf

shadowwonder 49 0 PDF 2018-12-25 18:12:22

Pro Hibernate and MongoDB.pdf,精通Hibernate和 MongoDB 英文完整版Contents at a glanceAbout the authorAbout the technical reviewerAcknowledgments mmmmmannmmaaaaammamaamnmmamaamamnammmmaann. Xviintroduction■■■■口■■■■XIXChapter 1: Getting Started with Hibernate OGM■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■口量Chapter 2: HibernateChapter 3: Bootstrapping Hibernate OGM37Chapter 4: Hibernate oGM at Workas■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■国■■51Chapter 5: Hibernate OGM and JPA 2.0 Annotations mmmmmmmmmmmmmmm 121Chapter 6: Hibernate OGM Querying MongoDB205Chapter 7: MongoDB e-Commerce Database Model■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■24Chapter 8: MongoDB e-Commerce Database Querying mammmamam maaaammanmaaaaan 269Chapter 9: Migrate MongoDB Database to cloud■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■283Chapter 10: Migrating Rafa EShop Application on Open Shift■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■口■■■■297Index…355IntroductionThis book covers all the important aspects of developing Hibernate OGM-MongoDB applications It provides clearinstructions for getting the most out of the Hibernate OGM-MongoDB duo and offers many examples of integratingHibernate ogm by means of both the hibernate native aPi and the java Persistence aPI. you will learn how todevelop desktop, web and enterprise applications for the most popular web and enterprise servers, such asTomcat, JBoss AS, and Glassfish AS You'll see how to take advantage of Hibernate OGM-MongoDB together withmany common technologies, such as JSE, Spring, Seam, EJB, and more. Finally, you'll learn how to migrate to thecloud-MongoHQ, MongoLab, and Open ShiftWho this book is forThis book is for experienced Java developers who are interested in exploring Hibernate solutions for NoSQL databasesFor the opening chapters( Chapters 1-3), it, s enough to be familiar with the main aspects of the orm paradigm,the Hibernate Native API, and JPA. The book provides brief overviews of these concepts. Starting with Chapter 4,you should have some knowledge about developing web applications(using NetBeans or Eclipse) deployed underthe Tomcat, JBOSs As, or GlassFish AS servers. Moreover, you need to be familiar with the Java technologies andframeworks that are commonly used in web applications, such as servlets, EJB, JSE, JSP, Seam, Spring, and so onHow This book is structuredHere's the main focus of each chapterChapter 1: Getting Started with Hibernate OGmThis chapter provides a brief introduction to the hibernate ogm world In the first part of the chapter, i discuss theseveral alternatives for downloading, installing, and configuring Hibernate OGM and MongoL ort. I then offerHibernate OGm architecture, its current features, and what we can expect in terms of future support. I then offerChapter 2: Hibernate oGM and mongoDBIn this chapter, i define more clearly the relationship between Hibernate ogm and mongodb by focusing on howHibernate ogm works with MongoDB. You learn how data is stored, how primary keys and associations are mappedand how to deal with transactions and queriesChapter 3: Bootstrapping Hibernate OGMThis chapter shows how Hibernate OGM can be bootstrapped by means of the Hibernate Native APi and JPaL INTRODUCTIONChapter 4: Hibernate OGM at WorkThis is one of the most important chapters You learn how to integrate hibernate ogm and mongodb in the mostcommon web and enterprise Java applications deployed on different servers. Here is the entire list of applicationsJava SE and Mongo DB-a"Hello world"exampleHibernate oGm(via Hibernate Native API)in a non-JTA environment dbc transactions,Tomcat 7)Hibernate OGM(via Hibernate Native API)in a standalone jTA environment Boss jta,Tomcat 7)Hibernate oGM(via Hibernate Native API)in a built-in jTA environment(no EJB, Glass Fish 3)Hibernate oGM(via Hibernate Native APi)in a built-in JTA environment(ejB/BMT, Glass Fish 3)Hibernate ogm(via Hibernate Native APi) in a built-in JTA environment(ejB/CMT, Glass Fish 3)Hibernate oGm (via JPA)in a built-in jTA environment(GlassFish AS 3)Hibernate oGm(via JPA)in a built-in ta environment ( Boss as 7)Hibernate oGm(via JPA)in a built-in JTA environment (Boss AS 7 and Seam application)Hibernate oGm(via JPA)in a built-in TA environment(Glass Fish and Spring application)Hibernate oGm(via JPA) JPA/JTA in a standalone jTA environment(tomcatHibernate OGm in a non-JTA environment(RESOURCE_LOCAL, Apache Tomcat 7)Chapter 5: Hibernate OGM and JPA 2.0 AnnotationsMapping Java entities in Hibernate OGM can be divided into supported and non-supported annotationsIn this chapter, I show the supported annotations, as well as how much of each annotation is supportedChapter 6: Hibernate oGm Querying mongoDBThis chapter explores the querying capabilities of Hibernate OGM. I start with a MongoDB native query and progressto complex queries written with Hibernate Search and Apache LuceneChapter 7: MongoDB e-Commerce Database ModelAt this point in the book, you will have acquired sufficent expertise to develop a real application that involvesHibernate oGm and MongoDB. An e-commerce web site is a good start and an interesting study case, so in thischapter i adapt a classic sQl database model to the Hibernate ogm and MongodB style. I also examine aspects ofe-commerce database architectureChapter 8: MongoDB e-Commerce Database QueryingAfter you develop a mongodb e-commerce database model, it' s time to sketch and implement the maine-commerce-specific queries In this chapter, I use Hibernate Search and apache lucene to write such queriesThe result is a complete e-commerce application named RafaEShopXXINTRODUCTIONChapter 9: Migrate MongoDB Database to CloudIn this chapter, you learn how to migrate the MongoDB e-commerce database developed in Chapter 7 into twoclouds: MongoHQ and MongoLabChapter 10: Migrating RafaEShop Application on Open ShiftThis final chapter is a detailed guide for migrating the e-commerce RafaEShop application to the Open Shift cloud ontwo enterprise servers: JBoss AS and Glass Fish asDownloading the codeThecodefortheexamplesshowninthisbookisavailableontheApresswebsitewww.apress.comYou'llfindthelinkon the books information page under the Source Code/Downloads tab. This tab is located underneath the RelatedTitles section of the pageContacting the AuthorShould you have any questions or comments-or even spot a mistake you think i should know about-you cancontactmeatleoprivacy@yahoo.comXⅩ1CHAPTER 1Getting Started with Hibernate OGMChances are, you're familiar with Hibernate ORM, a powerful, robust tool for converting data between relationaldatabases(RDBMS) and object-oriented programming languages. As an object-relational mapping ORM)framework, Hibernate ORM works with SQL Stores. In recent years, however, developers have become interested inNoSQL databases, which are optimized for storing and retrieving enormous quantities of data. NoSQL databases tendto be non-relational, open-source, horizontally scalable, distributed, and schema-freeThere are a number of ways to describe NosQl stores, but they are generally classified by data model, particularlythe followingDocument stores(Mongo DB, RavenDB, CouchDB and more)Wide column stores(Hypertable, Cassandra, HBase and moreKey value/tuple stores(DynamoDB, LevelDB, Redis, Ryak and moreGraph databases(Neo4J, GraphBase, Info Grid and moreThese are also commonMultimodel databases (OrientDB, ArangoDB and more)Object databases(db4o, Versant and more)Grid and cloud databases(GigaSpaces, Infinispan and more)XML databases(eXist, Sedna and moreClearly, NOSQL Stores are complex and very diverse. Some have significant user bases, while others are barelyknown. And each has its own strong points and weaknesses. You could even say that NosQL is such a keenly disputedtopic that programmers talk about it more than they actually use itThat's likely to change, however, with the recent release of the Hibernate OGM(Object Grid Mapper project,which offers a complete Java Persistence API (PA)engine for storing data in NoSQL stores. This project gives a realboost to Java developers looking to exploit NoSQL stores, since it provides a common interface-the well-knownJPA programming model-as a front end to various NoSQL approaches. Hibernate OGM is based on the HibernateORM Core engine, reuses the Java Persistence Query Language(P-QL) as an interface for querying stored data, andalready provides support for three NoSQL stores: MongoDB, Ehcache, and Infinispan, and Apache Cassandra shouldsee support in the future. Despite the youth of the project, the aims of the Hibernate OGm team guarantee it has hugepotential in the future-and a lot of work to accomplishCHAPTER 1 GETTING STARTED WITH HIBERNATE OGMFeatures and ExpectationsAs this book is written, the latest Hibernate ogm distribution is 4.0.0 Beta2 which already successfully providesa common interface for different NoSQL approaches; rapid scaling of a data store up or down; independence from theunderlying store technology and Hibernate Search. Here's what Hibernate OGM supports so farStoring data in document stores(MongoDB)storing data in key/value stores(Infinispan's data grid and EhcacheCreate, Read, Update and Delete (Crud) operations for JPA entitiesPolymorphic entities(support for superclasses, subclasses, and so forth)Embeddable objects(for example, embeddable classes, annotated in JPA with @Embeddable;collections of instances of embeddable classes, annotated in JPA with QElementCollection)Basic types(such as numbers, String, URL, Date, enumsAssociations(@Many Toone, @OneToOne, @One ToMany and @Many ToMany)Bidirectional associationsCollections(Set, List, Map, etc)Hibernate Search's full-text queriesJPA and native Hibernate ORM API(Hibernate OGM can be bootstrapped via JPA or viaHibernate Session, as I'll show you in Chapter 3.In the future, Hibernate oGm will support:Other key/value pair systemsOther NosQl enginesDeclarative denormalizationComplex JP-QL queries, including to-many joins and aggregationFronting existing JPA applicationsNote Denormalization is a database technique for speeding up the read process the idea is to reduce the number ofjoins in queries as much as possible; joins slow read performance because data must be picked up from multiple tableswithout disrupting their associations. While normalization promotes splitting related data into multiple associated tables,denormalization encourages adding a small number of redundancies to limit joins. Even if some data gets duplicatedperformance generally improvesHibernate ogm architectureBecause Hibernate ogm uses the existing Hibernate orm modules as much as possible the ogm architectureessentially extends the orm architecture by plugging different components in and out. Hibernate orm converts andpersists data between relational databases and object-oriented programming languages using a set of interfaces andclasses. These include the JDBC layer, used for connecting to databases and sending queries, and the Persistersand Loaders interfaces, responsible for persisting and loading entities and collections, as shown in Figure 1-1CHAPTER 1 GETTING STARTED WITH HIBERNATE OGMJNDIsoL Statements→ApplicationJDBC LayerDRIVERResultJTADatabaseFigure 1-1. Hibernate oRM architectureHibernate ogm is meant to accomplish the same goals, but using NoSQL stores. Thus, Hibernate ogm doesntneed the jDBC layer anymore and instead comes with two new elements: a datastore provider and a datastore dialect,as shown in Figure 1-2. Both of these act as adaptors between Hibernate oGM Core and the NoSQL store. (a datastoreis an adaptor that connects the core mapping engine with the specific NoSQl technology.世Datastore ProviderApplication乙山的三INTERACTIONz山的三GridDialectData baseFigure 1-2. Hibernate OGm datastore provider and datastore dialectThe datastore provider is responsible for managing connections to NoSQL stores, while the datastore dialectmanages communications with NoSQL Storage engines. Practically, these notions are materialized in two interfaces,org. hibernate ogm datastore. spi. DatastoreProvider and org. hibernate ogm dialect. GridDialect TheDatastoreProvider interface is responsible for starting, maintaining, and stopping a store connection, while theGridDialect interface deals with data persistence in NoSQL stores. Moreover, the Persisters and Loaders interfaceswere rewritten to support NOSQL Store featuresCurrently there are four implementations of DatastoreProviderEhcacheDatastoreProvider(for NoSQL Encache)InfinispanDatastoreProvider(for NoSQL InfinispanMongoDBDatastoreProvider (for NoSQL MongoDB)MapDatastoreProvider(for testing purposes)CHAPTER 1 GETTING STARTED WITH HIBERNATE OGMThere are five implementations of GridDialect for abstracting Hibernate OGM from a particular gridimplementation:EhcacheDialect(for EhCacheInfinispanDialect (for Infinispan)MongoDBDialect(for MongoDB)IshMapDialect(for testingGridDialectLogger(for logging calls performed on the real dialect)Note If you decide to write a new datastore, you have to implement a DatastoreProvider and a GridDialectFindmoredetailsaboutthisathttps://community.jbossorg/wiki/howtowriteadatasToreinhibeRnateOgmPersisting dataThrough the modified Loaders and Persisters interfaces, Hibernate ogm is capable of saving data to noSQL stores.Before doing so, however, OGm needs to represent and store the data internally. For this purpose, Hibernate ogmretains as much as it can of the relational database concepts and adapts these notions according to its needs Someconcepts, like storing entities, follow the relational model fairly completely, while others, like storing associations,do so partially. Data, therefore, is stored as basic types(entities are stored as tuples ) the notions of primary key andforeign key are still employed; and the application data model and the store data model relationships are abstractlymaintained through notions like table and columnOGM uses the tuple to represent the basic unit of data Tuples are meant to conceptually store entities as aMap. The key is the column name(the entity property/field or the @Column annotation value )andthe value is the column value as a primitive type(see Figure 1-3)ENtity@Table(name="players")Relational database modelHibernate oGM TuplesppknamesurnamebirthdaykeyvalueppkRafaelnadal03J.1986ppk2Novak22May,1987nameRafaelsuae3Federer8Aug1981surnameNadalTUPLEbirthdaRogerbirthdate03Jun,1986MapFigure 1-3. The Hibernate ogm tuple

Pro Hibernate and MongoDB.pdf

用户评论
请输入评论内容
评分:
暂无评论