SlideShare a Scribd company logo
Not Just ORM: Powerful Hibernate ORM Features and Capabilities
Not Just ORM:
Powerful Hibernate ORM
Features and Capabilities
Brett Meyer
Senior Software Engineer
Hibernate ORM, Red Hat
Brett Meyer
• Hibernate ORM
– ORM 4 & 5 development
– Hibernate OSGi
– Developer community engagement
– Red Hat support, Hibernate engineering lead

• Other contributions
– Apache Camel
– Infinispan

• Contact me
– @brettemeyer or +brettmeyer
– Freenode #hibernate or #hibernate-dev (brmeyer)
github.com/brmeyer
/HibernateDemos

slideshare.net/brmeyer
ORM? JPA?
• ORM: Object/Relational Mapping
– Persistence: Data objects outlive the JVM app
– Maps Java POJOs to relational databases
– Supports OO concepts: inheritance, object identity, etc.
– Navigate data by walking the object graph, not the explicit
relational model

• JPA: Java Persistence API
• Hibernate ORM provides its own native API, in
addition to full JPA support
• Annotations and XML
Overview
•
•
•
•
•
•
•
•
•

Multi-Tenancy
Value Generation
Hibernate Spatial
Hibernate Envers
Hibernate OSGi
Hibernate Search
Hibernate OGM & Validator
Hibernate Shards
Ask questions after each section!
Multi-Tenancy
Multi-Tenancy
•
•
•
•

1 ORM instance
Multiple concurrent clients
Data specific to each tenant
Legacy
– Separate SessionFactories for each tenant
– Application-provided Connections (given when
opening a Session)
– Big schemas or many tenants = huge footprint
Hibernate ORM
Multi-Tenancy
•
•
•
•

Strategy selection is global
Tenant identifier provided when opening a Session
Works with 2LC: identifier used in cache data key
Custom impls:
– MultiTenantConnectionProvider: required for physical
and schema separated (required)
– CurrentTenantIdentifierResolver: required for opening a
Session without providing the tenant id (explicitly or
Session#getCurrentSession)
Hibernate ORM
Multi-Tenancy Strategies
• Physically separated databases
– 1 JDBC connection pool per tenant
– Pool selected based on tenant identifier

• Single database, separate schemas
– Option #1
• Similar to physically separated
• 1 JDBC connection pool per tenant
• Provides schema through the connection URL or pool

– Option #2:
• 1 JDBC connection pool using a default schema
• Each Connection altered with SQL “SET SCHEMA” prior to use
Hibernate ORM
Multi-Tenancy Strategies
• Single database and schema
– Data partitioned by discriminator value
– Discriminator complexity varies
– Each SQL statement altered to include the
discriminator
– Not yet implemented (planned for ORM 5)
– Alternative: @FilterDef/@Filter w/ a
tenantId as a param
Hibernate ORM
Multi-Tenancy
• DEMO
• Questions?
Value Generation
Value Generation (Legacy)
• Limited to in-database value generation
on insert/update
• Properties marked with @Generated
immediately selected
Hibernate ORM
Value Generation
•
•
•
•

New, expanded support in ORM 4.3
Supports legacy in-DB generation, but also in-mem
Create custom annotations!
ORM provides built in generators
– @Generated: legacy, providing in-database generation
– @CreationTimestamp: set only once when owning entity is saved for
the first time
– @UpdateTimestamp: set any time owning entity is saved
– @GeneratorType: provide custom in-memory generator and define
“when”

• DEMO
• Questions?
Hibernate Spatial
Hibernate Spatial
• Currently 3rd party, but pulling in as a new ORM module in 5
• Originally created by Karel Maesen (geovise.com)
• Java Topology Suite (JTS)
– OpenGIS Simple Feature Spec (SFS) & SQL/MM Spatial (extends SFS)
– Oracle, Postgres, MySQL, MS SQL, H2, etc. all implement the specs
– Attempts to provide abstract, cross-platform geo data, but diffs between
them

• geolatte-geom
– Developed/maintained by Karel
– Improvement over JTS
– Fully interoperable with JTS
– Support for lat/lon geographies
– Additional Dialect differences
Hibernate Spatial (cont'd)
• Properties use JTS Geometry types
– Point
– LineString
– Polygon
– etc.

• Adds HQL functions for Dialect-supported methods
– Same functions also implemented as Criteria API Criterions
– ex: "select e from Event e where within(e.location, :filter) =
true" (:filter is a Geometry)
– http://www.hibernatespatial.org/documentation/03-dialects/0
1-overview/
Hibernate Spatial (cont'd)
• Translates between “Well-known text” (WKT) and
Geometry types
– Spatial object markup language
– 2D and 3D
– ex: “POINT (30 10)”, “POLYGON ((30 10, 40 40, 20 40, 10 20,
30 10))”

• Coordinate system transformations possible in queries
• Does not require JDBC extension drivers (ex: Oracle
SDOAPI.jar or Postgres postgis.jar)
• DEMO
• Questions?
Hibernate Envers
Hibernate Envers
• Provides historical versioning and auditing (“SCM for data”)
• Each transaction == a “revision”
• Revision #s are global --> querying for a historical snapshot
of the entire database is possible
• Requires
– @Audited on the entity or individual properties
– [entity]_AUD tables to store the historical data (DDL created
automated if using hbm2ddl, otherwise it’s exportable)

• Duplicate data? Yes.
• Powerful queries/capabilities > cheap mem
• Highly configurable
Hibernate Envers (cont'd)
• Revision info
– Default: simple internal entity stored in REVINFO table
– Revision # and revision timestamp
– Override with custom @RevisionEntity & RevisionListener
(add other useful fields: IP address, “blame”, etc.)

• Revision entity types
– Track the entity types that were changed in each revision
– Disabled by default: requires querying tables for changed
data
– Can be overridden by implementing
EntityTrackingRevisionListener on your RevisionEntity
Hibernate Envers (cont'd)
• Revision properties
– Track the entity properties that were changed in each revision
– Disabled by default: requires querying tables for changed data
– Adds boolean columns to audit tables

• Queries
– Snapshot of entity states at a given revision (horizontal)
– Revisions at which entities changed (vertical)

• DEMO
• Questions?
• Conditional auditing
– Default: Envers auditing reacts to Hibernate ORM events
– Disable hibernate.listeners.envers.autoRegister, create your own listeners,
wire them in using Integrator
Hibernate OSGi
Hibernate OSGi
• OSGi?
• Provide only the OSGi manifest and hacky workarounds <-NO!
• Emphasize doing things “the OSGi way”
– Dynamic environment
– Scoping
• Limited visibility into the container
• No scanning all bundles
• Reduced conflicts, allows concurrent instances, etc.

– OSGi services

• Isolated in hibernate-osgi module (no pervasive
dependencies or OSGi code)
Hibernate OSGi (cont'd)
• 3 supported environments
– Enterprise OSGi Managed JPA
• Container discovers and manages persistence units and EntityManagerFactories
• Similar to many app servers, Spring, etc.
• Ex: Apache Aries JPA

– Un-managed JPA
• Direct use of hibernate-entitymanager
• EntityManagerFactories created through OSGi services

– Native
• Direct use of hibernate-core
• SessionFactories created through OSGi services

• Quickstarts
• Questions?
Hibernate Search
Hibernate Search
• Full-text search on the DB
– Bad performance
– CPU/IO overhead

• Offload full-text queries to Hibernate Search
engine
– Fully indexed
– Horizontally scalable

• Based on Apache Lucene
• “Google for your entities”
Hibernate Search (cont'd)
• Annotate entities with @Indexed
• Annotate properties with @Field
– Index the text: index=Index.YES
– “Analyze” the text: analyze=Analyze.YES
•
•
•
•
•

Lucene analyzer
Chunks sentences into words
Lowercase all of them
Exclude common words (“a”, “the”)
Stemming
Hibernate OGM &
Hibernate Validator
Hibernate OGM & Validator
• Hibernate OGM:
– ORM/JPA support for NoSQL
– Infinispan, EHCache, MongoDB, Neo4j

• Hibernate Validator
– Bean Validation impl, but extended
– Both annotation and XML based
– @NotNull, @Size(min = 2, max = 14),
@Min(2), etc.
Hibernate Shards
Hibernate Shards
• Started as a Google team's 20% project
• Horizontal partitioning across multiple databases
• Flexible sharding strategies, both provided and
custom
• Supports virtual shards: simplifies re-sharding
• Typical Hibernate ORM usage: HQL, Criteria, etc.
• Last supported ORM version: 3.6.x
• Currently has upgrade momentum – contact
me if interested!
How to Help:
hibernate.org
/orm/contribute
Hibernate ORM:
Tips, Tricks, and
Performance Techniques

Tomorrow, 1pm,
Ballroom D
QUESTIONS?
•
•
•
•

Q&A
#hibernate or #hibernate-dev (brmeyer)
@brettemeyer
+brettmeyer

More Related Content

PPTX
Apache Spark Architecture
PDF
Hibernate ORM: Tips, Tricks, and Performance Techniques
ODP
ORM, JPA, & Hibernate Overview
PDF
High-Performance Hibernate Devoxx France 2016
PDF
Apache Spark Data Source V2 with Wenchen Fan and Gengliang Wang
PDF
Cosco: An Efficient Facebook-Scale Shuffle Service
PDF
Performant Streaming in Production: Preventing Common Pitfalls when Productio...
PDF
ELK Stack
Apache Spark Architecture
Hibernate ORM: Tips, Tricks, and Performance Techniques
ORM, JPA, & Hibernate Overview
High-Performance Hibernate Devoxx France 2016
Apache Spark Data Source V2 with Wenchen Fan and Gengliang Wang
Cosco: An Efficient Facebook-Scale Shuffle Service
Performant Streaming in Production: Preventing Common Pitfalls when Productio...
ELK Stack

What's hot (20)

PDF
Apache Flink internals
PPTX
Introduction to Storm
KEY
Introduction to memcached
PPTX
Hive: Loading Data
PDF
Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...
PDF
The Apache Spark File Format Ecosystem
PDF
CDC Stream Processing with Apache Flink
PPTX
Scripting robot
PDF
Building a Feature Store around Dataframes and Apache Spark
PPT
Hadoop Security Architecture
PPTX
ELK Elasticsearch Logstash and Kibana Stack for Log Management
PDF
Introducing ELK
PPTX
LLAP: long-lived execution in Hive
PDF
Facebook architecture
PPTX
Apache Solr
PDF
Spark SQL
PDF
The InnoDB Storage Engine for MySQL
PDF
[Outdated] Secrets of Performance Tuning Java on Kubernetes
PDF
Spark streaming , Spark SQL
ODP
Presto
Apache Flink internals
Introduction to Storm
Introduction to memcached
Hive: Loading Data
Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...
The Apache Spark File Format Ecosystem
CDC Stream Processing with Apache Flink
Scripting robot
Building a Feature Store around Dataframes and Apache Spark
Hadoop Security Architecture
ELK Elasticsearch Logstash and Kibana Stack for Log Management
Introducing ELK
LLAP: long-lived execution in Hive
Facebook architecture
Apache Solr
Spark SQL
The InnoDB Storage Engine for MySQL
[Outdated] Secrets of Performance Tuning Java on Kubernetes
Spark streaming , Spark SQL
Presto
Ad

Similar to Not Just ORM: Powerful Hibernate ORM Features and Capabilities (20)

PDF
Hibernate 3
PPTX
Hibernate in Action
PDF
Free Hibernate Tutorial | VirtualNuggets
PPT
Why hibernater1
PPT
Basic Hibernate Final
PPT
PPT
Hibernate Tutorial
PPTX
Ups and downs of enterprise Java app in a research setting
PPT
PPTX
Module-3 for career and JFSD ppt for study.pptx
PPTX
Hibernate in XPages
PPT
Patni Hibernate
DOCX
What is hibernate?
PPT
Hibernate jj
PPT
Hibernate
PPT
Hibernate introduction
PPTX
Hibernate tutorial
PDF
Hibernate complete notes_by_sekhar_sir_javabynatara_j
PDF
Hibernate complete notes_by_sekhar_sir_javabynatara_j
PDF
What is hibernate?
Hibernate 3
Hibernate in Action
Free Hibernate Tutorial | VirtualNuggets
Why hibernater1
Basic Hibernate Final
Hibernate Tutorial
Ups and downs of enterprise Java app in a research setting
Module-3 for career and JFSD ppt for study.pptx
Hibernate in XPages
Patni Hibernate
What is hibernate?
Hibernate jj
Hibernate
Hibernate introduction
Hibernate tutorial
Hibernate complete notes_by_sekhar_sir_javabynatara_j
Hibernate complete notes_by_sekhar_sir_javabynatara_j
What is hibernate?
Ad

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Approach and Philosophy of On baking technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPT
Teaching material agriculture food technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Encapsulation theory and applications.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Dropbox Q2 2025 Financial Results & Investor Presentation
Review of recent advances in non-invasive hemoglobin estimation
sap open course for s4hana steps from ECC to s4
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Mobile App Security Testing_ A Comprehensive Guide.pdf
MYSQL Presentation for SQL database connectivity
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Approach and Philosophy of On baking technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Teaching material agriculture food technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
The AUB Centre for AI in Media Proposal.docx
Chapter 3 Spatial Domain Image Processing.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

Not Just ORM: Powerful Hibernate ORM Features and Capabilities

  • 2. Not Just ORM: Powerful Hibernate ORM Features and Capabilities Brett Meyer Senior Software Engineer Hibernate ORM, Red Hat
  • 3. Brett Meyer • Hibernate ORM – ORM 4 & 5 development – Hibernate OSGi – Developer community engagement – Red Hat support, Hibernate engineering lead • Other contributions – Apache Camel – Infinispan • Contact me – @brettemeyer or +brettmeyer – Freenode #hibernate or #hibernate-dev (brmeyer)
  • 5. ORM? JPA? • ORM: Object/Relational Mapping – Persistence: Data objects outlive the JVM app – Maps Java POJOs to relational databases – Supports OO concepts: inheritance, object identity, etc. – Navigate data by walking the object graph, not the explicit relational model • JPA: Java Persistence API • Hibernate ORM provides its own native API, in addition to full JPA support • Annotations and XML
  • 6. Overview • • • • • • • • • Multi-Tenancy Value Generation Hibernate Spatial Hibernate Envers Hibernate OSGi Hibernate Search Hibernate OGM & Validator Hibernate Shards Ask questions after each section!
  • 8. Multi-Tenancy • • • • 1 ORM instance Multiple concurrent clients Data specific to each tenant Legacy – Separate SessionFactories for each tenant – Application-provided Connections (given when opening a Session) – Big schemas or many tenants = huge footprint
  • 9. Hibernate ORM Multi-Tenancy • • • • Strategy selection is global Tenant identifier provided when opening a Session Works with 2LC: identifier used in cache data key Custom impls: – MultiTenantConnectionProvider: required for physical and schema separated (required) – CurrentTenantIdentifierResolver: required for opening a Session without providing the tenant id (explicitly or Session#getCurrentSession)
  • 10. Hibernate ORM Multi-Tenancy Strategies • Physically separated databases – 1 JDBC connection pool per tenant – Pool selected based on tenant identifier • Single database, separate schemas – Option #1 • Similar to physically separated • 1 JDBC connection pool per tenant • Provides schema through the connection URL or pool – Option #2: • 1 JDBC connection pool using a default schema • Each Connection altered with SQL “SET SCHEMA” prior to use
  • 11. Hibernate ORM Multi-Tenancy Strategies • Single database and schema – Data partitioned by discriminator value – Discriminator complexity varies – Each SQL statement altered to include the discriminator – Not yet implemented (planned for ORM 5) – Alternative: @FilterDef/@Filter w/ a tenantId as a param
  • 14. Value Generation (Legacy) • Limited to in-database value generation on insert/update • Properties marked with @Generated immediately selected
  • 15. Hibernate ORM Value Generation • • • • New, expanded support in ORM 4.3 Supports legacy in-DB generation, but also in-mem Create custom annotations! ORM provides built in generators – @Generated: legacy, providing in-database generation – @CreationTimestamp: set only once when owning entity is saved for the first time – @UpdateTimestamp: set any time owning entity is saved – @GeneratorType: provide custom in-memory generator and define “when” • DEMO • Questions?
  • 17. Hibernate Spatial • Currently 3rd party, but pulling in as a new ORM module in 5 • Originally created by Karel Maesen (geovise.com) • Java Topology Suite (JTS) – OpenGIS Simple Feature Spec (SFS) & SQL/MM Spatial (extends SFS) – Oracle, Postgres, MySQL, MS SQL, H2, etc. all implement the specs – Attempts to provide abstract, cross-platform geo data, but diffs between them • geolatte-geom – Developed/maintained by Karel – Improvement over JTS – Fully interoperable with JTS – Support for lat/lon geographies – Additional Dialect differences
  • 18. Hibernate Spatial (cont'd) • Properties use JTS Geometry types – Point – LineString – Polygon – etc. • Adds HQL functions for Dialect-supported methods – Same functions also implemented as Criteria API Criterions – ex: "select e from Event e where within(e.location, :filter) = true" (:filter is a Geometry) – http://www.hibernatespatial.org/documentation/03-dialects/0 1-overview/
  • 19. Hibernate Spatial (cont'd) • Translates between “Well-known text” (WKT) and Geometry types – Spatial object markup language – 2D and 3D – ex: “POINT (30 10)”, “POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))” • Coordinate system transformations possible in queries • Does not require JDBC extension drivers (ex: Oracle SDOAPI.jar or Postgres postgis.jar) • DEMO • Questions?
  • 21. Hibernate Envers • Provides historical versioning and auditing (“SCM for data”) • Each transaction == a “revision” • Revision #s are global --> querying for a historical snapshot of the entire database is possible • Requires – @Audited on the entity or individual properties – [entity]_AUD tables to store the historical data (DDL created automated if using hbm2ddl, otherwise it’s exportable) • Duplicate data? Yes. • Powerful queries/capabilities > cheap mem • Highly configurable
  • 22. Hibernate Envers (cont'd) • Revision info – Default: simple internal entity stored in REVINFO table – Revision # and revision timestamp – Override with custom @RevisionEntity & RevisionListener (add other useful fields: IP address, “blame”, etc.) • Revision entity types – Track the entity types that were changed in each revision – Disabled by default: requires querying tables for changed data – Can be overridden by implementing EntityTrackingRevisionListener on your RevisionEntity
  • 23. Hibernate Envers (cont'd) • Revision properties – Track the entity properties that were changed in each revision – Disabled by default: requires querying tables for changed data – Adds boolean columns to audit tables • Queries – Snapshot of entity states at a given revision (horizontal) – Revisions at which entities changed (vertical) • DEMO • Questions? • Conditional auditing – Default: Envers auditing reacts to Hibernate ORM events – Disable hibernate.listeners.envers.autoRegister, create your own listeners, wire them in using Integrator
  • 25. Hibernate OSGi • OSGi? • Provide only the OSGi manifest and hacky workarounds <-NO! • Emphasize doing things “the OSGi way” – Dynamic environment – Scoping • Limited visibility into the container • No scanning all bundles • Reduced conflicts, allows concurrent instances, etc. – OSGi services • Isolated in hibernate-osgi module (no pervasive dependencies or OSGi code)
  • 26. Hibernate OSGi (cont'd) • 3 supported environments – Enterprise OSGi Managed JPA • Container discovers and manages persistence units and EntityManagerFactories • Similar to many app servers, Spring, etc. • Ex: Apache Aries JPA – Un-managed JPA • Direct use of hibernate-entitymanager • EntityManagerFactories created through OSGi services – Native • Direct use of hibernate-core • SessionFactories created through OSGi services • Quickstarts • Questions?
  • 28. Hibernate Search • Full-text search on the DB – Bad performance – CPU/IO overhead • Offload full-text queries to Hibernate Search engine – Fully indexed – Horizontally scalable • Based on Apache Lucene • “Google for your entities”
  • 29. Hibernate Search (cont'd) • Annotate entities with @Indexed • Annotate properties with @Field – Index the text: index=Index.YES – “Analyze” the text: analyze=Analyze.YES • • • • • Lucene analyzer Chunks sentences into words Lowercase all of them Exclude common words (“a”, “the”) Stemming
  • 31. Hibernate OGM & Validator • Hibernate OGM: – ORM/JPA support for NoSQL – Infinispan, EHCache, MongoDB, Neo4j • Hibernate Validator – Bean Validation impl, but extended – Both annotation and XML based – @NotNull, @Size(min = 2, max = 14), @Min(2), etc.
  • 33. Hibernate Shards • Started as a Google team's 20% project • Horizontal partitioning across multiple databases • Flexible sharding strategies, both provided and custom • Supports virtual shards: simplifies re-sharding • Typical Hibernate ORM usage: HQL, Criteria, etc. • Last supported ORM version: 3.6.x • Currently has upgrade momentum – contact me if interested!
  • 35. Hibernate ORM: Tips, Tricks, and Performance Techniques Tomorrow, 1pm, Ballroom D