0% found this document useful (0 votes)
95 views

Hibernate

The document discusses logging SQL statements in Hibernate and avoiding performance issues like N+1 queries. It provides examples of configuring a DataSource proxy to log SQL and using Hypersistence Optimizer to detect eager fetching. It also gives tips for Hibernate performance tuning like using lazy fetching where possible.

Uploaded by

Sakthivel P
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Hibernate

The document discusses logging SQL statements in Hibernate and avoiding performance issues like N+1 queries. It provides examples of configuring a DataSource proxy to log SQL and using Hypersistence Optimizer to detect eager fetching. It also gives tips for Hibernate performance tuning like using lazy fetching where possible.

Uploaded by

Sakthivel P
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Logging SQL statements

<logger name="org.hibernate.SQL" level="debug"/>

DataSource-proxy/P6spy

@Bean
public DataSource dataSource() {
SLF4JQueryLoggingListener loggingListener = new SLF4JQueryLoggingListener();
loggingListener.setQueryLogEntryCreator(new InlineQueryLogEntryCreator());
return ProxyDataSourceBuilder
.create(actualDataSource())
.name(DATA_SOURCE_PROXY_NAME)
.listener(loggingListener)
.build();
}

Name:DATA_SOURCE_PROXY, Time:6, Success:True,


Type:Prepared, Batch:True, QuerySize:1, BatchSize:3,
Query:["insert into post (title, version, id) values (?, ?, ?)"],
Params:[(Post no. 0, 0, 0), (Post no. 1, 0, 1), (Post no. 2, 0, 2)]

--------------------------------------------------------------------------
<dependency>
<groupId>io.hypersistence</groupId>
<artifactId>hypersistence-optimizer</artifactId>
<version>${hypersistence-optimizer.version}</version>
</dependency>

@Test
public void testNoPerformanceIssues() {
HypersistenceOptimizer hypersistenceOptimizer = new HypersistenceOptimizer(
new JpaConfig(entityManagerFactory())
);

assertTrue(hypersistenceOptimizer.getEvents().isEmpty());
}
ERROR [main]: Hypersistence Optimizer - CRITICAL
- EagerFetchingEvent
- The [post] attribute in the [io.hypersistence.optimizer.config.PostComment]
entity
uses eager fetching. Consider using a lazy fetching which,
not only that is more efficient, but it is way more flexible
when it comes to fetching data.

For more info about this event, check out this User Guide link
- https://vladmihalcea.com/hypersistence-optimizer/docs/user-
guide/#EagerFetchingEvent

java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
at
io.hypersistence.optimizer.config.FailFastOnPerformanceIssuesTest.testNoPerformance
Issues(FailFastOnPerformanceIssuesTest.java:41)

==========================================================

Hibernate performance tuning tips


refer using lazy fetching and keep in mind that @ManyToOne and @OneToOne
association are fetched eagerly by default.
there is no way to switch the fetch strategy from EAGER to LAZY even if you are
using JPA entity graphs.
if you forget to JOIN FETCH an EAGER association in a JPQL or Criteria API query,
you’ll end up with an N+1 query issue.

Association type Default fetching policy


@OneToMany LAZY
@ManyToMany LAZY
@ManyToOne EAGER
@OneToOne EAGER

DTO projection using JPA Tuple and JPQL


DTO projections using a Constructor Expression and JPQL
DTO projections using Tuple and native SQL queries
DTO projections using ResultTransformer and JPQL
DTO projections using ResultTransformer and a Native SQL query

spring.jpa.properties.hibernate.session.events.log.LOG_QUERIES_SLOWER_THAN_MS=25

==============================================================
“Hibernate : Mistakes to avoid” https://link.medium.com/AfVzxBvXj5

“Hibernate N+1 Queries Problem” https://link.medium.com/TQSInMIYj5

“JPA/Hibernate Bidirectional Lazy Loading Done Right”


https://link.medium.com/SFOO5fnZj5

“Hibernate and the n+1 selections problem” https://link.medium.com/CduWJlpZj5

You might also like