Java Persistence.pdf | High-performance

  • Query cache – Caches JPQL/HQL results. Use only for static data.
  • When to avoid caching – Frequently updated data.
  • | Anti-pattern | Consequence | |-------------|-------------| | @OneToMany with CascadeType.ALL + eager fetch | N+1 queries + large joins | | Open Session in View (OSIV) | Long-running DB transactions | | Using wrapper types in GROUP BY | Surprising null behavior | | Not defining equals()/hashCode() on entities | Broken collections in detached state | | Using merge() instead of persist() | Unnecessary select before insert |

    High-performance Java persistence cannot ignore the database engine itself. High-performance Java Persistence.pdf

    Before blaming Hibernate for slow queries, look at the underlying mechanism: JDBC. A significant portion of latency in Java persistence comes not from the query execution itself, but from the data transfer between the application and the database. Query cache – Caches JPQL/HQL results

    Hibernate will not create the perfect index for you automatically. Understanding that an index on (created_at) is useless for a query filtering by (status) is crucial. You must analyze your query execution plans (using EXPLAIN ANALYZE) to ensure your database is seeking, not scanning. High-performance Java Persistence.pdf

    Most developers stop at setting a URL and password. High-performance Java Persistence.pdf dedicates significant space to tuning the connection pool (HikariCP, the gold standard).

    An e-commerce site saw timeouts during Black Friday. The team found that loading a ShoppingCart entity triggered lazy loading of CartItem, Product, Discount, and Inventory across 50 queries. After applying the "Dynamic Fetching" strategies from High-performance Java Persistence.pdf, they reduced the transaction to 2 queries and a single JOIN FETCH. Time per request dropped from 4 seconds to 50ms.