Created by Petr "Glubo" Sykora
SELECT * FROM article;
SELECT * FROM author WHERE id = 1;
SELECT * FROM author WHERE id = 2;
-- ...
SELECT article.*, author.*
FROM article
LEFT JOIN author ON article.author_id = article.id;
SELECT * FROM article;
SELECT * FROM author WHERE id IN (...);
application.yml:
...
spring:
jpa:
show-sql: true
properties:
hibernate:
format_sql: true
Hibernate:
select
aem1_0.id,
aem1_0.author_id,
aem1_0.name,
aem1_0.perex
from
article aem1_0
application.yml:
...
spring:
jpa:
properties:
hibernate:
generate_statistics: true
10:10:06.322 [pool-1-thread-1 @coroutine#7] INFO o.h.e.i.StatisticalLoggingSessionEventListener - Session Metrics {
5950 nanoseconds spent acquiring 1 JDBC connections;
7000 nanoseconds spent releasing 1 JDBC connections;
361002 nanoseconds spent preparing 23 JDBC statements;
4345252 nanoseconds spent executing 23 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
1954341 nanoseconds spent executing 1 flushes (flushing a total of 224 entities and 0 collections);
9386541973450 nanoseconds spent executing 1 pre-partial-flushes;
1770 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
}
10:10:06.324 [pool-1-thread-1 @coroutine#7] INFO PgStatsService - Query statistics:
calls: 22 totalExec: 0s totalPlan: 0s query: 'select aem1_0.id,aem1_0.name from author aem1_0 where aem1_0.id=$1'
calls: 1 totalExec: 0s totalPlan: 0s query: 'SELECT 1 FROM pg_stat_statements_reset()'
calls: 1 totalExec: 0s totalPlan: 0s query: 'select aem1_0.id,aem1_0.author_id,aem1_0.name,aem1_0.perex from article aem1_0'
calls: 2 totalExec: 0s totalPlan: 0s query: 'BEGIN'
...
@ManyToOne(fetch = FetchType.EAGER)
var author: AuthorBatchSizeMTO,
...
...
@Table(name = "article")
@Entity
@BatchSize(size = 100)
class Article(
@ManyToOne()
@BatchSize(size = 100)
var author: AuthorBatchSizeMTO,
...
...
@Fetch(FetchMode.JOIN)
var author: Author,
...
...
@Repository
interface Repo : CrudRepository<Article, Long> {
@EntityGraph(attributePaths = [ "author" ])
abstract override fun findAll(): MutableList<Article>
...
@Entity
@NamedEntityGraph(
name = "myDescriptiveName",
attributeNodes = [
NamedAttributeNode("author"),
],
)
class Article(
...
@Repository
interface Repo : CrudRepository<Article, Long> {
@EntityGraph(value = "myDescriptiveName")
abstract override fun findAll(): MutableList<Article>
...
...
@Repository
interface Repo : CrudRepository<Article, Long> {
@Query(
"select a" +
" from Article a" +
" join fetch a.author",
)
fun joinFetch(): MutableList<Article>
...
...
fun findAllFetch(): List {
val builder = em.criteriaBuilder
val query = builder.createQuery(ArticleDefaultWinMTO::class.java)
val entity = query.from(ArticleDefaultWinMTO::class.java)
entity.fetch("author")
return em.createQuery(query).resultList
}
...