O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mastering Hibernate

Book Description

Learn how to correctly utilize the most popular Object-Relational Mapping tool for your Enterprise application

About This Book

  • Understand the internals of Hibernate and its architecture, and how it manages Entities, Events, Versioning, Filters, and Cache
  • Observe how Hibernate bridges the gap between object-oriented concepts and relational models
  • Discover how Hibernate can address architectural concerns such as Transaction, Database Multi-tenancy, Clustering, and Database Shards

Who This Book Is For

Mastering Hibernate is intended for those who are already using or considering using Hibernate as the solution to address the problem of Object Relational Mapping. If you are already using Hibernate, this book will help you understand the internals and become a power user of Hibernate.

What You Will Learn

  • Understand the internals of a Hibernate session and how Entities are managed
  • Declare better mapping between entity classes and database tables
  • Manage entity associations and collections
  • Fetch data not just by entity ID, but also using HQL, Criteria Objects, Filters, and Native SQL
  • Observe the first and second level caches and find out how to manage them
  • Collect statistics and metrics data for further observation
  • Make your application work with multi-tenant databases

In Detail

Hibernate has been so successful since its inception that it even influenced the Java Enterprise Edition specification in that the Java Persistence API was dramatically changed to do it the Hibernate way. Hibernate is the tool that solves the complex problem of Object Relational Mapping. It can be used in both Java Enterprise applications as well as .Net applications. Additionally, it can be used for both SQL and NoSQL data stores.

Some developers learn the basics of Hibernate and hit the ground quickly. But when demands go beyond the basics, they take a reactive approach instead of learning the fundamentals and core concepts. However, the secret to success for any good developer is knowing and understanding the tools at your disposal. It’s time to learn about your tool to use it better

This book first explores the internals of Hibernate by discussing what occurs inside a Hibernate session and how Entities are managed. Then, we cover core topics such as mapping, querying, caching, and we demonstrate how to use a wide range of very useful annotations.

Additionally, you will learn how to create event listeners or interceptors utilizing the improved architecture in the latest version of Hibernate.

Style and approach

This book takes a close look at the core topics, and helps you understand the complex topics by showing you examples and giving you in-depth discussions.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Mastering Hibernate
    1. Table of Contents
    2. Mastering Hibernate
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Entity and Session
      1. Why this book?
      2. Quick Hibernate
      3. Working with a session
        1. Session internals
        2. Contextual session
        3. Session per request
        4. Session per conversation
        5. Session per operation
        6. Stateless session
      4. Entity
        1. Entity lifecycle
        2. Types of entities
        3. Identity crisis
        4. Beyond JPA
      5. Proxy objects
      6. Batch processing
        1. Manual batch management
        2. Setting batch size
        3. Using stateless session
      7. Summary
    9. 2. Advanced Mapping
      1. Mapping concepts
        1. Instances and rows
        2. Annotation versus XML
        3. Owning entity
        4. Value mapping
        5. JPA ID generation
        6. Hibernate ID generation
        7. Composite ID
      2. Association cardinality
        1. One-to-one associations
        2. One-to-many associations
        3. Many-to-one associations
        4. Many-to-many associations
        5. Self-referencing tables
      3. Cascade operations
      4. Inheritance
        1. Single table strategy
        2. Table per class strategy
        3. Joined strategy
      5. Enumeration and custom data type
        1. Enumerated type
        2. Custom data type mapping
      6. Summary
    10. 3. Working with Annotations
      1. Mapping and association
        1. @Any and @ManyToAny
        2. @MapsId
        3. @Fetch
        4. @OrderBy
        5. @ElementCollection and @CollectionTable
      2. Behavior
        1. @Cache
        2. @Access
        3. @Cascade
        4. @CreationTimestamp and @UpdateTimestamp
        5. @Filter and @FilterDef
        6. @Immutable
        7. @Loader
        8. @NotFound
        9. @SortComparator and @SortNatural
      3. SQL/DDL modifier
        1. @Check
        2. @ColumnDefault
        3. @ColumnTransformer
        4. @DynamicInsert and @DynamicUpdate
        5. @Formula
        6. @SelectBeforeUpdate
        7. @SQLDelete and @Where
        8. @SQLInsert and @SQLUpdate
        9. @SubSelect and @Synchronize
        10. @WhereJoinTable
        11. @NaturalId
      4. Summary
    11. 4. Advanced Fetching
      1. Fetching strategy
        1. The JOIN fetch mode
        2. The SELECT fetch mode
        3. The SUBSELECT fetch mode
        4. Batch fetching
      2. Hibernate Query Language
        1. Fetch queries
        2. Delete and update
        3. Join
      3. Native SQL
        1. Scalar query
        2. Entity query
      4. Criteria objects
      5. Filters
      6. Pagination
      7. Summary
    12. 5. Hibernate Cache
      1. Cache structure
        1. Cache scope
        2. First-level cache
        3. Second-level cache
          1. Cache provider interface
          2. Ehcache implementation
          3. Cache configuration
        4. Query cache
      2. Caching benefits and pitfalls
      3. Caching strategies
        1. Read only
        2. Non-strict read write
        3. Read-write
        4. Transactional
        5. Object identity
      4. Managing the cache
        1. Remove cached entities
        2. Cache modes
      5. Cache metrics
      6. Summary
    13. 6. Events, Interceptors, and Envers
      1. Services
        1. Service loader
        2. Service registry
        3. The OSGi model
      2. Events
        1. Event listener
        2. Registering listeners
      3. Interceptors
        1. Database trigger
        2. Event or interceptor
      4. Envers
        1. Configuration
        2. Strategy
        3. Fetching revisions
      5. Summary
    14. 7. Metrics and Statistics
      1. Statistical data types
        1. Session
        2. Entity
        3. Collection
        4. Query
        5. Cache
      2. Statistics via JMX
        1. Introduction to JMX
        2. Using JMX with Hibernate
      3. Summary
    15. 8. Addressing Architecture
      1. Architecture matters
      2. Transaction management
        1. Local transactions
        2. The Java Transaction API
        3. Compensating transactions
      3. Concurrency
        1. Isolation levels
        2. Locking
        3. User lock
      4. Scalability
        1. Clustering
        2. Database shards
          1. Configuration
          2. Sharded session factory
          3. Shard strategy
          4. Shard ID generation
      5. Performance
        1. Lazy loading
        2. Fetch mode
        3. Batch processing
        4. Caching
        5. Stateless session
      6. Legacy application
        1. Reverse engineering
        2. Modernization
      7. The Cloud strategy
        1. Licensing
        2. Multi-tenancy
      8. Summary
    16. 9. EJB and Spring Context
      1. Deployment
        1. Configuration
        2. Resource
        3. Transaction
        4. Hibernate libraries
      2. EJB
        1. Persistence unit
        2. Server resources
        3. Entity manager
        4. Transaction
        5. Hibernate session
      3. Spring
        1. Configuration
        2. Transaction management
        3. Data source
        4. Session factory
      4. Summary
    17. Index