Hibernate ORM

repository·main·Indexed 27 days ago

https://github.com/hibernate/hibernate-orm

A Java object/relational mapping solution implementing Jakarta Persistence. This documentation covers development and testing workflows, including managing database containers with Docker or Podman Compose, implementing and profiling JMH benchmarks for ActionQueue performance, and using the Hibernate Maven Plugin for hbm2java and hbm2orm goals.

Tokens
149.3K
Snippets
248
Records
764
Agent score
89%

What's inside hibernate-orm

  1. Overview of Hibernate 8 and Jakarta Specifications

    main

    Hibernate 8 is a modern ORM solution for Java that implements the latest Jakarta specifications. It provides high levels of compile-time type safety and feature breadth for object-relational mapping.

    Key specifications supported include:

    • Jakarta Persistence 4.0: Allows accessing the full power of Hibernate via standard API interfaces with an improved programming model.
    • Jakarta Data 1.1: Integrated with Jakarta Persistence to simplify handling dynamic queries.
    • Jakarta Query 1.0: Provides a rigorous definition of the query language.

    Hibernate 8 is a core component of Quarkus 3 for cloud-native Java development.

  2. Overview of Hibernate Assistant

    main

    The Hibernate Assistant module acts as a bridge between Hibernate ORM applications and generative AI services. It provides building blocks to expose your domain model and database operations to Large Language Models (LLMs), enabling natural language interactions with your data layer.

    Note: This module is currently incubating and may experience breaking changes at any time, including in micro (patch) releases.

  3. Overview of Hibernate development tasks

    main

    Developing a program with Hibernate typically involves the following workflow:

    1. Configuring and bootstrapping: Setting up Hibernate and obtaining an EntityManagerFactory.
    2. Defining the domain model: Creating entity classes that map to database tables.
    3. Customizing mappings: Adjusting mappings to work with pre-existing relational schemas.
    4. Performing operations: Using EntityManager or EntityAgent to query and update data.
    5. Improving type-safety: Using Hibernate Processor for compile-time checks.
    6. Writing queries: Using Hibernate Query Language (HQL) or native SQL.
    7. Performance tuning: Optimizing data access logic.
  4. Overview of Hibernate ORM

    main
    Hibernate ORM is a Java object/relational mapping (ORM) solution that implements Jakarta Persistence (formerly JPA), Jakarta Query, and Jakarta Data. It allows developers to work with relational data in a type-safe way, manage complex queries, synchronize in-memory changes with a database, and handle transactions, temporal data, audit logging, multi-tenancy, and row-level security.
  5. Overview of Hibernate Data Repositories

    main

    Hibernate Data Repositories is an implementation of the Jakarta Data specification. It provides a way to interact with relational databases using a typesafe repository interface.

    Key characteristics include:

    • Mapping: Entity classes are mapped using Jakarta Persistence annotations.
    • Querying: Queries can be written in Hibernate Query Language (HQL), which is a superset of Jakarta Persistence Query Language (JPQL).
    • Programming Model: It uses a repository-based approach (interfaces exposing APIs for datastore interaction) which differs from the traditional Jakarta Persistence model.
  6. Getting Started with Hibernate

    main

    Hibernate is an Object/Relational Mapping (ORM) solution for Java environments that maps Java classes to database tables and Java data types to SQL data types. It reduces manual data handling using SQL and JDBC by providing data query and retrieval facilities.

    For new users, it is recommended to follow the Quick Start guide for a tutorial-style introduction. For a high-level discussion of core features, refer to the Introduction to Hibernate guide.

  7. Overview of Hibernate Reverse Engineering

    main

    Hibernate Reverse Engineering reads an existing database schema via JDBC to generate various artifacts. This is useful for bootstrapping a Hibernate project from a legacy database or keeping generated code in sync with an evolving schema.

    Available Exporters:

    • Java entity classes: Annotated Jakarta Persistence entity classes.
    • Data Access Objects (DAOs): Classes providing basic CRUD operations.
    • hbm.xml mapping files: Hibernate XML mapping files.
    • hibernate.cfg.xml: A configuration file listing the generated mappings.
    • DDL scripts: SQL schema creation and drop scripts.
    • HTML documentation: An HTML report describing the database schema.
    • Custom FreeMarker templates: User-defined templates for arbitrary code generation.

    Integration details for specific build tools (Gradle, Maven, Ant) are available in their respective documentation sections.

  8. Understand SQL AST Translation in Hibernate

    main

    Hibernate uses SQL AST (Abstract Syntax Tree) translators to convert a SQL AST tree into executable JDBC operations. The translator is dialect-aware and designed for single-use. It does not execute the SQL itself; instead, it produces objects (like JdbcSelect or JdbcMutationOperation) that are later consumed by JDBC executors.

    The translation process follows this flow:

    1. A SqlAstTranslatorFactory creates a SqlAstTranslator.
    2. The SqlAstTranslator walks the SQL AST.
    3. During the walk, it renders the SQL string and collects JDBC parameters.
    4. Finally, it produces a JdbcOperation (the executable operation).
  9. Understand Foreign Key Mapping in the Metamodel

    main

    In Hibernate's metamodel, a single foreign key (FK) is represented by one ForeignKeyDescriptor. This descriptor contains two sides: the keySide (the side holding the foreign key column) and the targetSide (the side being referenced).

    For a bidirectional relationship, two separate Association references are created, both pointing to the same ForeignKeyDescriptor but specifying different sides:

    • The key-side association (e.g., ManyToOne) models the side containing the actual database column.
    • The target-side association (e.g., OneToMany) models the side being pointed to.

    This structure allows Hibernate to resolve join predicates for HQL/JPQL queries regardless of which entity is used as the starting point of the join.

  10. Understand the relationship between Criteria and SQM

    main
    In Hibernate, the Criteria implementation does not build a separate model. Instead, Criteria objects are directly instantiated as SQM (Semantic Query Model) objects. When you call the Criteria API, Hibernate creates and wires SQM nodes directly. This means that Criteria Path, Root, Join, Expression, and Predicate objects are actually SQM nodes, allowing Criteria and HQL to share the same query planning, parameter handling, and SQL translation logic.
  11. Understand Hibernate configuration setting namespaces

    main

    Hibernate configuration settings are categorized into three distinct namespaces based on their origin and standardization level:

    1. Jakarta Persistence: Standardized settings for any persistence provider, defined by the jakarta.persistence. namespace. This is the current standard.
    2. Hibernate: Hibernate-specific settings that control behaviors extending beyond the Jakarta Persistence specification, defined by the hibernate. namespace.
    3. Legacy JPA: Settings from the older Java Persistence specification (prior to version 3.1), defined by the javax.persistence. namespace.

    Recommendation: When configuring your application, always prefer the jakarta.persistence. namespace over the legacy javax.persistence. namespace. Hibernate provides support for both, but legacy support may be removed in the future.