You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/src/main/asciidoc/introduction/Introduction.adoc
+51-3Lines changed: 51 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -412,10 +412,10 @@ So now let's talk about how to organize persistence logic in a real system.
412
412
The rest of this chapter is not compulsory.
413
413
If you're itching for more details about Hibernate itself, you're quite welcome to skip straight to the <<configuration,next chapter>>, and come back later.
414
414
415
-
[[entities]]
415
+
[[domain-model]]
416
416
=== Entities
417
417
418
-
A class in the domain model which directly represents a relational database table is called an _entity_.
418
+
A class in the domain model which directly represents a relational database table is called an <<entities,_entity_>>.
419
419
Entity classes are central to object persistence and to object/relational mapping.
420
420
They're also, typically, central players in the business logic of our application program.
421
421
Entities represent the _things_ in our business domain.
@@ -464,6 +464,54 @@ But the basic principle remains intact: an entity does not do orchestration, it
464
464
465
465
For now, we're going to assume that entities are implemented as plain Java classes.
466
466
467
+
[[stateful-and-stateless-sessions]]
468
+
=== Stateful and stateless sessions
469
+
470
+
It should be very clear from the example code <<hello-hibernate,above>>, that the session is also a very important object.
471
+
It exposes basic operations like `persist()` and `createQuery()`, and so it's our first port of call when we want to <<interacting,interact with the database>> via Hibernate.
472
+
In the code we just saw, we've used a _stateful session_.
473
+
474
+
Later, we'll learn about the idea of a <<persistence-contexts,_persistence context_>>.
475
+
Oversimplifying for now, you can think of it as a cache of data which has been read in the current transaction.
476
+
Thus, in the architecture of Hibernate, it's sometimes called the _first-level cache_.
477
+
Each stateful session -- that is, every Hibernate `Session`, and every JPA <<hibernate-and-jpa,`EntityManager`>> -- has its own persistence context.
478
+
479
+
But stateful sessions have never been the only possibility.
480
+
The <<stateless-sessions,`StatelessSession`>> interface offers a way to interact with Hibernate _without_ going through a persistence context.
481
+
However, the programming model is somewhat different.
482
+
483
+
.Stateless sessions
484
+
****
485
+
Among our biggest regrets is that we didn't give enough love to `StatelessSession` twenty years ago.
486
+
Sure, a stateful session is in some sense more powerful, or at least more magical.
487
+
But with that magic comes a loss of direct control over persistence operations, and some traps for inexperienced users.
488
+
A significant minority of developers find working with a persistence context frustrating, and they would surely be better served by a stateless session.
489
+
490
+
- We used to view `StatelessSession` as an API directed toward very specific usage patterns, in particular, batch processing of large numbers of entities.
491
+
As a result, we left out certain functionality -- for example, use of the <<second-level-cache,second-level cache>> -- which didn't seem relevant to those use cases.
492
+
This left `StatelessSession` lacking feature parity with `Session`, and it was a mistake.
493
+
In Hibernate 7, we've fixed this mistake.
494
+
A `StatelessSession` now offers essentially all the functionality of Hibernate except, naturally, the first-level cache.
495
+
496
+
- Compounding our error, we left `StatelessSession` out of JPA.
497
+
This meant that a large number of Hibernate users _didn't even realize this option existed._
498
+
We promise to make sure there are stateless sessions in Jakarta Persistence 4.
499
+
500
+
So, finally, let us state for the record: we messed up here.
501
+
Hibernate is all about _object/relational mapping;_ persistence contexts are something extra on top.
502
+
You don't have to use stateful sessions, and you're not doing anything wrong if you decide to use stateless sessions instead.
503
+
****
504
+
505
+
As of Hibernate 7, a key decision for any new project is which of these programming models to take as a baseline.
506
+
Fortunately, the two models aren't mutually exclusive.
507
+
This is a friendly competition, where the two APIs are designed to complement each other.
508
+
Even if we decide to use `StatefulSession` most of the time, we can still use a `StatelessSession` wherever it's more convenient.
509
+
510
+
NOTE: On the other hand, if you decide to adopt Jakarta Data, the decision is made for you: repositories in Jakarta Data 1.0 are always stateless, and in https://hibernate.org/repositories/[Hibernate Data Repositories] a repository is backed by a `StatelessSession`.
511
+
512
+
But now we've got just a little bit ahead of ourselves.
513
+
In the next section taking we're taking a journey which _might_ -- but definitely doesn't _necessarily_ -- end at the idea of a "repository".
514
+
467
515
[[organizing-persistence]]
468
516
=== Organizing persistence logic
469
517
@@ -714,7 +762,7 @@ At the time we wrote _An Introduction to Hibernate 6_, we were especially frustr
714
762
In our considered opinion, such frameworks typically made JPA harder to use, sometimes misleading users into misuse of the technology.
715
763
716
764
The birth of the Jakarta Data specification has obsoleted our arguments against repositories, along with the older frameworks which were the source of our frustration.
717
-
Jakarta Data--as realized by Hibernate Data Repositories--offers a clean but very flexible way to organize code, along with much better compile-time type safety, without getting in the way of direct use of the `StatelessSession`.
765
+
Jakarta Data--as realized by Hibernate Data Repositories--offers a clean but very flexible way to organize code, along with much better compile-time type safety, without getting in the way of direct use of the <<stateless-sessions,`StatelessSession`>>.
718
766
****
719
767
720
768
Now that we have a rough picture of what our persistence logic might look like, it's natural to ask how we should test our code.
0 commit comments