Skip to content

Commit 03e6c74

Browse files
committed
first draft of "general advice" section
1 parent 84a1f22 commit 03e6c74

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

documentation/src/main/asciidoc/introduction/Configuration.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ Alternatively, the `SchemaManager` API allow you to control schema export progra
338338
sessionFactory.getSchemaManager().exportMappedObjects(true);
339339
====
340340

341-
341+
[[logging-generated-sql]]
342342
=== Logging the generated SQL
343343

344344
:log4j: https://github.com/hibernate/hibernate-reactive/blob/main/examples/session-example/src/main/resources/log4j2.properties

documentation/src/main/asciidoc/introduction/Interacting.adoc

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,4 +787,54 @@ The `Connection` passed to the work is the same connection being used by the ses
787787
You can call stored procedures using `createStoredProcedureQuery()` or `createStoredProcedureCall()`.
788788
====
789789

790-
If the work returns a value, use `doReturningWork()` instead of `doWork()`.
790+
If the work returns a value, use `doReturningWork()` instead of `doWork()`.
791+
792+
[[advice]]
793+
=== What to do when things go wrong
794+
795+
Object/relational mapping has been called the "Vietnam of computer science".
796+
The person who made this analogy is American, and so one supposes that he meant to imply some kind of unwinnable war.
797+
This is quite ironic, since at the very moment he made this comment, Hibernate was already on the brink of winning the war.
798+
799+
Today, Vietnam is a peaceful country with exploding per-capita GDP, and ORM is a solved problem.
800+
That said, Hibernate is complex, and ORM still presents many pitfalls for the inexperienced, even occasionally for the experienced.
801+
Sometimes things go wrong.
802+
803+
In this section we'll quickly sketch some general strategies for avoiding "quagmires".
804+
805+
- Understand SQL and the relational model.
806+
Know the capabilities of your RDBMS.
807+
Work closely with the DBA if you're lucky enough to have one.
808+
Hibernate is not about "transparent persistence" for Java objects.
809+
It's about making two excellent technologies work smoothly together.
810+
- <<logging-generated-sql,Log the SQL>> executed by Hibernate.
811+
// Look, this seems obvious, until you've met users who didn't realize it was possible or useful.
812+
You cannot know that your persistence logic is correct until you've actually inspected the SQL that's being executed.
813+
Even when everything seems to be "working", there might be a lurking <<association-fetching,N+1 selects monster>>.
814+
- Be careful when <<many-to-one,modifying bidirectional associations>>.
815+
In principle, you should update _both ends_ of the association.
816+
But Hibernate doesn't strictly enforce that, since there are some situations where such a rule would be too heavy-handed.
817+
Whatever the case, it's up to you to maintain consistency across your model.
818+
- Never <<persistence-contexts,leak persistence context>> across threads or concurrent transactions.
819+
Have a strategy or framework to guarantee this never happens.
820+
- When running queries that return large result sets, take care to consider the size of the <<session-cache-management,session cache>>.
821+
Consider using a <<stateless-sessions,stateless session>>.
822+
- Think carefully about the semantics of the <<second-level-cache,second-level cache>>, and how the caching policies impact transaction isolation.
823+
- Avoid fancy bells and whistles you don't need.
824+
Hibernate is incredibly feature-rich, and that's a good thing, because it serves the needs of a huge number of users, many of whom have one or two very specialized needs.
825+
But nobody has _all_ those specialized needs.
826+
In all probability, you have none of them.
827+
Write your domain model in the simplest way that's reasonable, using the simplest mapping strategies that make sense.
828+
- When something isn't behaving as you expect, _simplify_.
829+
Isolate the problem.
830+
Find the absolute minimum test case which reproduces the behavior, _before_ asking for help online.
831+
Most of the time, the mere act of isolating the problem will suggest an obvious solution.
832+
- Avoid frameworks and libraries that "wrap" JPA.
833+
If there's any one criticism of Hibernate and ORM that sometimes _does_ ring true, it's that it takes you too far from direct control over JDBC.
834+
An additional layer just takes you even further.
835+
- Avoid copy/pasting code from random bloggers or stackoverflow reply guys.
836+
Many of the suggestions you'll find online just aren't the simplest solution, and many aren't correct for Hibernate 6.
837+
Instead, _understand_ what you're doing; study the Javadoc of the APIs you're using; read the JPA specification; follow the advice we give in this document; go direct to the Hibernate team on Zulip.
838+
(Sure, we can be a bit cantankerous at times, but we _do_ always want you to be successful.)
839+
- Always consider other options.
840+
You don't have to use Hibernate for _everything_.

0 commit comments

Comments
 (0)