Mastering Hibernate
eBook - ePub

Mastering Hibernate

  1. 204 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering Hibernate

About this book

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.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Mastering Hibernate


Table of Contents

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

Mastering Hibernate

Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: May 2016
Production reference: 1100516
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78217-533-9
www.packtpub.com

Credits

Author
Ramin Rad
Reviewers
Luca Masini
Sherwin John Calleja-Tragura
Aurélie Vache
Commissioning Editor
Neil Alexander
Acquisition Editor
Reshma Raman
Content Development Editor
Zeeyan Pinheiro
Technical Editor
Pranjali Mistry
Copy Editor
Pranjali Chury
Project Coordinator
Francina Pinto
Proofreader
Safis Editing
Indexer
Rekha Nair
Production Coordinator
Manu Joseph
Cover Work
Manu Joseph

About the Author

Ramin Rad has been working as a software professional for the last 20 years, living in Washington DC area. He studied mathematics and computer science in the academic world, and he cares deeply for the art of software development.
He is also a musician, a guitarist, who performs regularly in the DC area and beyond. His artistic side is significant because he believes that writing software is more art than science, following the philosophy of the likes of Donald Knuth, who wrote the Art of Computer Programming series.
Currently, he is a director and a solution architect at a large international software product and services company, focusing on enterprise, mobile, and cloud-based solutions.

About the Reviewers

Luca Masini is a senior software engineer and architect. He started off as a game developer for Commodore 64 (Football Manager) and Commodore Amiga (Ken il guerriero). He soon converted to object oriented programming, and for that, from its beginning in 1995, he was attracted by the Java language.
Following his passion, he worked as a consultant for major Italian banks, developing and integrating the main software projects for which he has often taken technical leadership. He encouraged the adoption Java Enterprise in environments where COBOL was the flagship platform, converting them from mainframe centric to distributed.
He then shifted his focus toward open source, starting from Linux and then m...

Table of contents

  1. Mastering Hibernate

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Mastering Hibernate by Ramin Rad in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Java. We have over one million books available in our catalogue for you to explore.