SlideShare a Scribd company logo
Hibernate and Spring - Unleash the Magic
Victor Rentea
VictorRentea.ro
Recorded Talks, Blog, Video Courses, ...
Independent Trainer
Founder of
Bucharest Software Craftsmanship Community
Java Champion
❤️ Simple Design, Refactoring, Unit Testing ❤️
Training
500 days
(200+ online)
2000 devs
8 years 50+ companies
Reach out
to me:
Hibernate
Spring Func Prog
Java Performance
Reactive Prog
Design Patterns
Pragmatic DDD
Clean Code
Refactoring
Unit Testing
TDD
@victorrentea
Intense
Video Courses: victorrentea.teachable.com
More: VictorRentea.ro
VictorRentea.ro
74
¡¡ PLEASE !!
Ask questions!
Hibernate and Spring - Unleash the Magic
Get Set ....
• JPA Auditing
• Domain Events
• javax.validation
• Spring Data Repos
JPA Auditing
• @CreatedBy / Date
• @LastModifiedBy / Date
• Relies on @PrePersist, @PreUpdate JPA Hooks defined via
• @EntityListeners on @Entity or
• global via orm.xml
• User is retrieved from an AuditorAware
• Typically from Spring Security Session (SecurityContextHolder)
Domain Events (DDD)
• Problem: logic inside an @Entity methods needs to trigger further
processing, involving other unrelated Aggregates
• At .save(e), the Entity is scanned for a @DomainEvents getter
• Pitfall: What if I forget to .save()?
• Pitfall: Auto-flushing the dirty entity does NOT publish the events
➔Alternative: static EventPublisherHolder
• Events are delivered to all interested @EventListener
• In the same transaction (default, safest)
• After the current transaction COMMITS - @TransactionalEventListener
javax.validation
• Before writing to DB, @Entities are automatically validated:
• @NotNull
• @Size(min=)
• @Pattern(regexp=)
• ...
• Custom Validators (even loaded from Spring)
CustomerRepository
Customer findByName(String name);
@Query("SELECT c FROM Cust... ")
List<Customer> getActiveCustomers();
JpaRepository<E,ID>
List<E> findAll();
E findOne(ID);
void save(E);
void delete(E); ...
CustomerRepositoryCustom
List<Customer> search(CustomerSearch);
CustomerRepositoryImpl
List<Customer> search(CustomerSearch) {
...<implem>...
}
CustomBaseRepository<E,ID>
E findExactlyOne(ID id);
CustomBase
RepositoryImpl
<implem>
naming
convention
Spring Data Repo Architecture
JpaSpecificationExecutor<E>
List<E> findAll(Specification<E> spec);
QuerydslPredicateExecutor<E>
List<E> findAll(Predicate spec);
Codegeneration
via
Maven Plugin
@NonNullApi
on ...repo/package-info.java
Repo method cannot return null anymore
You have to return Optional<>
Finish!
• JPA Auditing
• Domain Events
• javax.validation
• Spring Data Repos
If you decide to use magic
make sure THE ENTIRE team understands it
Hibernate and Spring - Unleash the Magic

More Related Content

PDF
The Proxy Fairy and the Magic of Spring @JAX Mainz 2021
PDF
The Proxy Fairy, and The Magic of Spring Framework
PDF
Pure Functions and Immutable Objects
PDF
Don't Be Mocked by your Mocks - Best Practices using Mocks
PPTX
Clean Pragmatic Architecture - Avoiding a Monolith
PDF
Unit Testing like a Pro - The Circle of Purity
PPTX
Functional Patterns with Java8 at Devoxx UK - Slides
PDF
Clean pragmatic architecture @ devflix
The Proxy Fairy and the Magic of Spring @JAX Mainz 2021
The Proxy Fairy, and The Magic of Spring Framework
Pure Functions and Immutable Objects
Don't Be Mocked by your Mocks - Best Practices using Mocks
Clean Pragmatic Architecture - Avoiding a Monolith
Unit Testing like a Pro - The Circle of Purity
Functional Patterns with Java8 at Devoxx UK - Slides
Clean pragmatic architecture @ devflix

What's hot (20)

PDF
Definitive Guide to Working With Exceptions in Java - takj at Java Champions ...
PDF
Clean architecture - Protecting the Domain
PPTX
Clean Code - The Next Chapter
PDF
Refactoring blockers and code smells @jNation 2021
PDF
Refactoring Games - 15 things to do after Extract Method
PDF
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
PDF
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
PDF
The Art of Unit Testing - Towards a Testable Design
PDF
Clean Lambdas & Streams in Java8
PPTX
Clean Code with Java 8 - Functional Patterns and Best Practices
PPTX
Clean Code
PDF
Definitive Guide to Working With Exceptions in Java
PPTX
Functional Patterns with Java8 @Bucharest Java User Group
PDF
Integration testing with spring @snow one
PDF
Integration testing with spring @JAX Mainz
PDF
Extreme Professionalism - Software Craftsmanship
PPTX
Functional Programming Patterns with Java 8 (at Devoxx BE)
PPTX
A Tale About the Evil Partial Mock and Separation by Layers of Abstractions
PPTX
Clean Lambdas at JBCNConf by Victor Rentea
PDF
Living With Legacy Code
Definitive Guide to Working With Exceptions in Java - takj at Java Champions ...
Clean architecture - Protecting the Domain
Clean Code - The Next Chapter
Refactoring blockers and code smells @jNation 2021
Refactoring Games - 15 things to do after Extract Method
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
The Art of Unit Testing - Towards a Testable Design
Clean Lambdas & Streams in Java8
Clean Code with Java 8 - Functional Patterns and Best Practices
Clean Code
Definitive Guide to Working With Exceptions in Java
Functional Patterns with Java8 @Bucharest Java User Group
Integration testing with spring @snow one
Integration testing with spring @JAX Mainz
Extreme Professionalism - Software Craftsmanship
Functional Programming Patterns with Java 8 (at Devoxx BE)
A Tale About the Evil Partial Mock and Separation by Layers of Abstractions
Clean Lambdas at JBCNConf by Victor Rentea
Living With Legacy Code
Ad

Similar to Hibernate and Spring - Unleash the Magic (20)

PDF
TDD Mantra
PDF
Micronaut Deep Dive - Devoxx Belgium 2019
PPTX
Dealing with and learning from the sandbox
PPTX
TALK-I-TIVE_PresentatTALK-I-TIVE_Presentation[1][1].pptxion[1][1].pptx
PPTX
TALK-I-TIVE_Presensadsdasdasdasdasdasdastation[1].pptx
PPTX
Introduction to Module Development with Appcelerator Titanium
PPTX
Codestrong 2012 breakout session how to develop your own modules
PPTX
Dealing with and learning from the sandbox
PPTX
Java ee 8 + security overview
PDF
VA Smalltalk Update
PPTX
Migrate to microservices
PDF
Software Craftsmanship @Code Camp Festival 2022.pdf
PPTX
Code your Own: Authentication Provider for Blackboard Learn
PPTX
An api is not "yet another feature"
PPTX
SignalR and GroomingPoker
PDF
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
PPTX
Sitecore upgrade best practices
DOC
vidyullathasree_pera_resume
PDF
QuerySurge for DevOps
PDF
Django Developer Certification
TDD Mantra
Micronaut Deep Dive - Devoxx Belgium 2019
Dealing with and learning from the sandbox
TALK-I-TIVE_PresentatTALK-I-TIVE_Presentation[1][1].pptxion[1][1].pptx
TALK-I-TIVE_Presensadsdasdasdasdasdasdastation[1].pptx
Introduction to Module Development with Appcelerator Titanium
Codestrong 2012 breakout session how to develop your own modules
Dealing with and learning from the sandbox
Java ee 8 + security overview
VA Smalltalk Update
Migrate to microservices
Software Craftsmanship @Code Camp Festival 2022.pdf
Code your Own: Authentication Provider for Blackboard Learn
An api is not "yet another feature"
SignalR and GroomingPoker
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Sitecore upgrade best practices
vidyullathasree_pera_resume
QuerySurge for DevOps
Django Developer Certification
Ad

More from Victor Rentea (17)

PDF
Top REST API Desgin Pitfalls @ Devoxx 2024
PDF
The Joy of Testing - Deep Dive @ Devoxx Belgium 2024
PDF
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
PDF
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
PDF
Microservice Resilience Patterns @VoxxedCern'24
PDF
Distributed Consistency.pdf
PDF
Clean Code @Voxxed Days Cluj 2023 - opening Keynote
PDF
Testing Microservices @DevoxxBE 23.pdf
PPTX
From Web to Flux @DevoxxBE 2023.pptx
PPTX
Test-Driven Design Insights@DevoxxBE 2023.pptx
PDF
Profiling your Java Application
PPTX
OAuth in the Wild
PPTX
The tests are trying to tell you something@VoxxedBucharest.pptx
PPTX
Vertical Slicing Architectures
PDF
Unit testing - 9 design hints
PPTX
Extreme Professionalism - Software Craftsmanship
PDF
Pure functions and immutable objects @dev nexus 2021
Top REST API Desgin Pitfalls @ Devoxx 2024
The Joy of Testing - Deep Dive @ Devoxx Belgium 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Microservice Resilience Patterns @VoxxedCern'24
Distributed Consistency.pdf
Clean Code @Voxxed Days Cluj 2023 - opening Keynote
Testing Microservices @DevoxxBE 23.pdf
From Web to Flux @DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptx
Profiling your Java Application
OAuth in the Wild
The tests are trying to tell you something@VoxxedBucharest.pptx
Vertical Slicing Architectures
Unit testing - 9 design hints
Extreme Professionalism - Software Craftsmanship
Pure functions and immutable objects @dev nexus 2021

Recently uploaded (20)

PDF
Softaken Excel to vCard Converter Software.pdf
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Introduction to Artificial Intelligence
PDF
AI in Product Development-omnex systems
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
medical staffing services at VALiNTRY
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Essential Infomation Tech presentation.pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
System and Network Administraation Chapter 3
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Softaken Excel to vCard Converter Software.pdf
How Creative Agencies Leverage Project Management Software.pdf
Introduction to Artificial Intelligence
AI in Product Development-omnex systems
PTS Company Brochure 2025 (1).pdf.......
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Design an Analysis of Algorithms II-SECS-1021-03
Upgrade and Innovation Strategies for SAP ERP Customers
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
medical staffing services at VALiNTRY
Understanding Forklifts - TECH EHS Solution
Essential Infomation Tech presentation.pptx
top salesforce developer skills in 2025.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
How to Migrate SBCGlobal Email to Yahoo Easily
Navsoft: AI-Powered Business Solutions & Custom Software Development
System and Network Administraation Chapter 3
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free

Hibernate and Spring - Unleash the Magic

  • 2. Victor Rentea VictorRentea.ro Recorded Talks, Blog, Video Courses, ... Independent Trainer Founder of Bucharest Software Craftsmanship Community Java Champion ❤️ Simple Design, Refactoring, Unit Testing ❤️
  • 3. Training 500 days (200+ online) 2000 devs 8 years 50+ companies Reach out to me: Hibernate Spring Func Prog Java Performance Reactive Prog Design Patterns Pragmatic DDD Clean Code Refactoring Unit Testing TDD @victorrentea Intense Video Courses: victorrentea.teachable.com More: VictorRentea.ro
  • 6. Get Set .... • JPA Auditing • Domain Events • javax.validation • Spring Data Repos
  • 7. JPA Auditing • @CreatedBy / Date • @LastModifiedBy / Date • Relies on @PrePersist, @PreUpdate JPA Hooks defined via • @EntityListeners on @Entity or • global via orm.xml • User is retrieved from an AuditorAware • Typically from Spring Security Session (SecurityContextHolder)
  • 8. Domain Events (DDD) • Problem: logic inside an @Entity methods needs to trigger further processing, involving other unrelated Aggregates • At .save(e), the Entity is scanned for a @DomainEvents getter • Pitfall: What if I forget to .save()? • Pitfall: Auto-flushing the dirty entity does NOT publish the events ➔Alternative: static EventPublisherHolder • Events are delivered to all interested @EventListener • In the same transaction (default, safest) • After the current transaction COMMITS - @TransactionalEventListener
  • 9. javax.validation • Before writing to DB, @Entities are automatically validated: • @NotNull • @Size(min=) • @Pattern(regexp=) • ... • Custom Validators (even loaded from Spring)
  • 10. CustomerRepository Customer findByName(String name); @Query("SELECT c FROM Cust... ") List<Customer> getActiveCustomers(); JpaRepository<E,ID> List<E> findAll(); E findOne(ID); void save(E); void delete(E); ... CustomerRepositoryCustom List<Customer> search(CustomerSearch); CustomerRepositoryImpl List<Customer> search(CustomerSearch) { ...<implem>... } CustomBaseRepository<E,ID> E findExactlyOne(ID id); CustomBase RepositoryImpl <implem> naming convention Spring Data Repo Architecture JpaSpecificationExecutor<E> List<E> findAll(Specification<E> spec); QuerydslPredicateExecutor<E> List<E> findAll(Predicate spec); Codegeneration via Maven Plugin
  • 11. @NonNullApi on ...repo/package-info.java Repo method cannot return null anymore You have to return Optional<>
  • 12. Finish! • JPA Auditing • Domain Events • javax.validation • Spring Data Repos
  • 13. If you decide to use magic make sure THE ENTIRE team understands it