Hibernate
Раздел про Hibernate и JPA для Java простыми словами: ORM и маппинг сущностей, связи, persistence context, ленивая загрузка и N+1, JPQL и Criteria, кэширование, транзакции и блокировки, наследование и типичные грабли.
A from-scratch section on Hibernate — the most common JPA implementation. How Java objects map to tables, how the persistence context works, where LazyInitializationException and the N+1 problem come from, how to write queries, and how to avoid the typical ORM pitfalls. Deeper than the Spring Data overview — best read in order.
- What ORM and Hibernate are: from JDBC to object-relational mapping — In plain terms: what ORM is, why you need it, how JPA differs from Hibernate, when ORM helps and when it gets in the way, and how it all fits into Spring Boot.
- Entity mapping in Hibernate and JPA — How Hibernate links Java classes to tables: @Entity, @Column, ID generation, @Enumerated, @Embedded and @Transient — with a breakdown of the typical traps.
- Associations between entities in Hibernate: @OneToMany, @ManyToOne, @ManyToMany — We break down @ManyToOne, @OneToMany, @ManyToMany, the owning side, mappedBy, and syncing bidirectional associations in Hibernate/JPA.
- The persistence context and entity states in Hibernate — What the persistence context is, how Hibernate tracks changes automatically, and which states an entity passes through — from creation to removal.
- Lazy and eager loading in Hibernate — What FetchType.LAZY and EAGER are, how proxies and lazy collections work, why LazyInitializationException happens, and how to get rid of it properly.
- The N+1 query problem in Hibernate — What the N+1 problem is, how to spot the extra queries, and how to eliminate them: JOIN FETCH, @EntityGraph, batch loading, DTO projections.
- JPQL, the Criteria API and native queries in Hibernate — We break down the three ways to write queries in JPA: JPQL — over entities, the Criteria API — for dynamic filters, Native SQL — when the ORM cannot cope.
- Caching in Hibernate: first level, second level and the query cache — How Hibernate caches data at the session level and between sessions, why you need the second level, when it is harmful, and how the query cache works.
- Transactions, flush and locking in Hibernate — How Hibernate manages transaction boundaries, when a flush happens, and how optimistic locking via @Version differs from pessimistic SELECT FOR UPDATE.
- Entity inheritance in Hibernate: SINGLE_TABLE, JOINED, TABLE_PER_CLASS — How Hibernate maps a class hierarchy to tables: three strategies, @MappedSuperclass, and practical advice on choosing an approach.
- Typical mistakes with Hibernate: what goes wrong and how to fix it — A breakdown of the frequent Hibernate pitfalls: wrong equals/hashCode, Open Session in View, returning entities from a controller, the dangerous CascadeType.ALL, bulk operations done one by one, and merge vs save.